tarlejh Posted March 3, 2011 Share Posted March 3, 2011 Hi, I need some clarification of a simple O-O concept. Within a class (in the example below for instance), does the "$this" variable refer to the object, or a does it only refer to a copy of the object? Thanks. <?php class Foo { public $prop; public function bar(){ echo $this->prop; } } ?> Quote Link to comment Share on other sites More sharing options...
btherl Posted March 3, 2011 Share Posted March 3, 2011 self::$my_static is the static class variable $my_static, $this->my_var is specific to the current copy (aka instance) of the object. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 3, 2011 Share Posted March 3, 2011 btherl's reply brings up some interesting aspects, but I thought I'd expand purely on the question of $this. A class is like DNA. It's not a thing yet, but it's the blueprint for the thing. You make an object using the "new" keyword. $myFoo = new Foo(); So the question in regards to what $this-> does inside class code, is that it represents "this object", allowing you to refer to its variables and methods. At runtime the values will reflect the object that was created by new. Of course you can have many Foo() objects, each with its own properties. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.