Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics Building the Recipe Separating Methods

Obayanju Damilare
Obayanju Damilare
3,383 Points

when do we use self ?? thanks for your answer

when and why do we use the self keyword

2 Answers

Mochammad Rezza
Mochammad Rezza
4,778 Points

self used when we using static property and want to access or set current static property.

for example I have Instance class in the following code.

class Instance
{
     private static $_instance;

     public static function getInstance(){

        if(!isset(self::$_instance)){  

           self::$_instance =  new Database;  // set variable $_instance with new object connection

                 }
       }
}
Obayanju Damilare
Obayanju Damilare
3,383 Points

Thanks Rezza, so basically its used to call another static method within a class