terryds Posted June 23, 2013 Share Posted June 23, 2013 I often found variable $this, but until now, i still don't understand about that....I still see that in functions and OOP.. Can you explain about that to me? Link to comment Share on other sites More sharing options...
birbal Posted June 23, 2013 Share Posted June 23, 2013 It is special variable, instance variable. It does reprerents the object itself when used inside object context. Link to comment Share on other sites More sharing options...
terryds Posted June 24, 2013 Author Share Posted June 24, 2013 See my code.. <?phpfunction bark() { print "{$this->Name} says Woof!\n";} bark(); The error appears : Fatal error: Using $this when not in object context in C:\xampp\htdocs\terrytest\scratches\$this.php on line 3I get the code from http://www.tuxradar.com/practicalphp/6/5/0 Can you tell me how to fix it? Or give me another simple example of using $this ? I still don't understand about it Link to comment Share on other sites More sharing options...
birbal Posted June 24, 2013 Share Posted June 24, 2013 $this->name tells to get 'name' property of current object. You neither have any class defined nor it have any property. You need to define a class first. Only class methods can resolve $this, functions can not. Link to comment Share on other sites More sharing options...
hengyuguodu Posted June 24, 2013 Share Posted June 24, 2013 class bus{ private speed; public function run() { $this->speed = 25; }}You can use like this Link to comment Share on other sites More sharing options...
thescientist Posted June 24, 2013 Share Posted June 24, 2013 See my code..<?phpfunction bark() { print "{$this->Name} says Woof!\n";} bark(); The error appears : Fatal error: Using $this when not in object context in C:\xampp\htdocs\terrytest\scratches\$this.php on line 3I get the code from http://www.tuxradar....cticalphp/6/5/0 Can you tell me how to fix it? Or give me another simple example of using $this ? I still don't understand about it the previous chapter setup the proper context for the current examplehttp://www.tuxradar.com/practicalphp/6/4/0 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now