1

I understand that $something->function(); is calling a function within a class but what does $something->somethingelse->function(); mean?

1
  • 1
    Are you sure it's $something->$somethingelse->function(); and not $something->somethingelse->function(); or $something->somethingelse()->function();? Commented Oct 1, 2011 at 13:29

3 Answers 3

1

I assume you mean $something->somethingelse->function().

It means that a property of $something named $somethingelse is another object that has the method function().

$something = new Class1();
$something->somethingelse = new Class2();  // Class2 has the "function()" method


// Now I can call...
$something->somethingelse->function();
Sign up to request clarification or add additional context in comments.

4 Comments

OH, so what is the purpose of this?
@user658911 The purpose is to have the functionality of that object inside that class. It's called the dependency injection pattern.
So would you use this for using a database object like $addusers->db = $db; where $db is a class.
@user yup, for example, exactly.
1

you can say it like this:

class foo {
  public $bar;
}

class bar {
  public $baz;
}

class baz {
  function blip() { echo "Hello World"; }
}

$foo = new foo();
$foo->bar = new bar(); // setting bar to new object
$foo->bar->baz = new baz(); // setting baz to new object
$foo->bar->baz->blip(); // calling the blip function

1 Comment

I changed it around, so it works exactly the same. I wasnt aware that var was deprecated - thanks
0

The class of the instance $something contains an instance variable called $somethingelse which is also an object (instance of a class).

It's considered a bad practice (see Law of Demeter on Wikipedia).

A nice explanation of the Law of Demeter is on c2.com of Peter Van Rooijen is:

You can play with yourself.

You can play with your own toys (but you can't take them apart),

You can play with toys that were given to you.

And you can play with toys you've made yourself.

That is in plain English:

Your method can call other methods in its class directly

Your method can call methods on its own fields directly (but not on the fields' fields)

When your method takes parameters, your method can call methods on those parameters directly.

When your method creates local objects, that method can call methods on the local objects.

BUT

One should not call methods on a global object

One should not have a chain of messages a.getB().getC().doSomething() in some class other than a's class.

Another interesting read is Law of Demeter Revisited for a less strict approach.

2 Comments

I disagree that it's bad practice generally. Patterns like Dependency Injection couldn't work without it
dependency injection though, is about injecting the instance of $somethingelse in $something, without having to create it with new inside $something (that would cement the dependency). It's not about exposing those dependencies, what should be the "private parts" of a class. You can make it write only with a setter.

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.