stefanosn
stefanosn

Reputation: 3324

php this variable

I have found this line in a php book is it right? I do not know if the 'this->objects[key]' is like this.

Here is the line:

this->objects[ $key ] = new $object( $this);

Is this a mistake by the author? I am talking about the 'this' variable.

Upvotes: 0

Views: 157

Answers (3)

Flinsch
Flinsch

Reputation: 4341

What's the concrete question? What "should" be wrong with that line of code?

Syntactically, it looks correct, except for one small thing: It should read $this instead of this.

This is what happens in this line: You have an instance attribute $this->objects of type array. The value entry identified by $key is initialized (or updated) by assigning a new object of class $object to it. The concrete name of that class is encoded (as a string) in the $object variable. For example: $object = 'MyCustomer' would result in new MyCustomer($this). A parameter ($this) is passed to the constructor, but that is another story and, as I assume, not subject of the current problem.

Maybe, hopefully, that is what you are asking for: new $object(...) means, for example, new MyCustomer(...). If not, I didn't get the point of your problem, sorry.

Upvotes: 1

Kae Verens
Kae Verens

Reputation: 4079

should be $this, not this.

let's hope it wasn't one of my books ;-)

Upvotes: 0

Czechnology
Czechnology

Reputation: 14992

I guess the author just forgot the dollar sign in front of the $this variable.

Upvotes: 4

Related Questions