0

I have a class function that queries data like so:

// Disclaimer: (None of the following is "real")
class Mockup {

  function getData() {

    query("SELECT id,name FROM pages")
    // Results: $id = 1, $name = "Math Text Book: Grade 7"

  }

}

Though I'd like to be able to access both values (That is, $id and $name), I can only return one value individually (Thus creating a disadvantage when compared to simply querying outside of the class).

Would something along the lines of the following be possible in conjunction with the above example code?

$test = new Mockup;
echo $test->getData()->id; // Echos "7"

Similarly, I've seen something along the lines of the following in various software such as MediaWiki:

$test = new Mockup;
echo $test->getData()->getId(); // Echos "7"

Is that an indication that it's also possible to nest functions as well?

2
  • 2
    Why can you only access one value? Is query() implicitly creating variables?! What about returning an array of data? It's very unclear to me what issues you have, you'll have to elaborate more. Querying data from a database isn't exactly uncharted territory, so what problem do you have with the standard solutions everyone's using? Commented Jan 13, 2015 at 3:25
  • @deceze Perhaps I wasn't clear - The point of the question was to find how to get the data needed using the "method" near the bottom (Hence why I assumed that they were "nested") - The fact that "I can't get more than one value" wasn't the question so that needn't matter. Commented Jan 13, 2015 at 4:09

1 Answer 1

1

That's not an embedded function it's method chaining. When a method returns the class instance, you can use it to immediately use another class member (var or function), thus chaining them together.

class Mockup {

  public $id;
  public $name;

  function getData() {
    $data = query();
    $this->id = $data->id;
    $this->name = $data->name;
    return $this; // return the instance for chaining
  }

  function getId() {
    return $this->id;
  }

  function getName() {
    return $this->name;
  }

}

$mockup = new Mockup();

//unchained
$mockup->getData();
$id = $mockup->getId();
$name = $mockup->getName();
// or
$id = $mockup->id;
$name = $mockup->name;

//chained
$id = $mockup->getData()->getId();
$name = $mockup->getData()->getName();
//or
$id = $mockup->getData()->id;
$name = $mockup->getData()->name;

If you want to return both at once you could simply return an array or an object containing those values.

FYI: When using chaining you don't have to return the instance of the class you are calling the method from, you can also in that method, create a new instance of another class and return that, so you can call members from that instance, for example:

$garage->getVehicle('plane')->fly(); // returns new Plane() with specific methods
$garage->getVehicle('car')->drive(); // returns new Car() with specific methods
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that makes much more sense! Hopefully you can see why I was confused. Thank-you for explaining that.

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.