0

I am building a custom data handler to save some precious bandwidth for my users. Problem is, I am trying to add an object on the fly, into an array.

It is not working as expected.

Any help would be appreciated

function getName() {
   return "test";
}


class Parser {

  private $results = [];

  public function parseCalls($callArray){
    for ($i=0; $i < count($callArray); $i++) {
      switch($callArray[$i]->call){
        case 'getName':
          $results[] = (object) ['result' => getName()]; // this is where it fails.
          break;
      }
    }
  }

  public function sendResult(){
    echo json_encode($this->results);
  }

 }

$parser = new Parser();


if(isset($_POST["callArray"])){
  // $_POST['callArray'] contains [{call: 'getName'}];
  $callsArray = json_decode($_POST["callArray"]);
  $parser->parseCalls($callsArray);

}

$parser->sendResult();

How do I make it that the results array contains something like this: [{result: "test"}]

Thank you very much! :)

2
  • The variable $results within the function is not defined but I assume it is meant to be the private property of the class - ie: $this->results[]=... Commented Sep 13, 2016 at 16:19
  • @RamRaider Ah, I didn't see that. Thanks a ton! This is what I was looking for. Commented Sep 13, 2016 at 16:21

1 Answer 1

1

You appear to now have solved that little conundrum but I wrote this and spotted the problem whilst doing so.

public function parseCalls( $calls=array() ){
    foreach( $calls as $i => $call ){
        try{
            switch( strtolower( $call ) ){
                case 'getname':
                    $this->results[]=(object)array( 'result'=>getName() );
                break;
            }
        }catch( Exception $e ){
            die( $e->getMessage() );
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.