0

I have an object $user.

var_dump($user) will output this:

object(user)#12 (1) {
  ["mylog"]=> object(mylog)#13 (2) { 
    ["userid"]=> string(1) "1" 
    ["uname"]=> string(5) "admin"
  }
}

What I want to access is the "userid" property. I succeeded with

foreach($user as $otherObject=>$property)
{
     echo $property->userid;
}

My question is if I can do something like $user->OtherObjectPlaceholder->userid without needing to loop through all the properties?

1
  • 1
    ... $user->mylog->userid Commented Jan 10, 2013 at 10:15

2 Answers 2

2

You can make chaining at php for methods if you return object. This is called Fluent Interface.

class a
{
  public $a;

  public function __construct()
  {
      $this->a = new b; 
  }
}

class b {
   public $b;
}

$object = new a;
$object->a->b
Sign up to request clarification or add additional context in comments.

Comments

0

create a getter in $user object and run this:

$user->getOtherObject()->userid

1 Comment

could you explain in more detail, I'm new to oop, how can I create a getter inside the object I already have?

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.