开发者

php this variable

开发者 https://www.devze.com 2023-02-13 07:15 出处:网络
I have found this line in a php book is it right? I do not know if the \'this->objects[key]\' is like this.

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 开发者_Go百科line:

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

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


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


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.


should be $this, not this.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消