static keyword example in oop


//well well !! welcome back again .. lets talk about static keyword
// static keyword are useable before properties and method .. static properties or method are //accessable at anywhere without instantiating ..

// and if someone want to use static method or properties in the same class where it has been made
// then he/she must maintain a syntax “self::function/properties name”
//Look at the below example will make it clear

class ollo
static public $a = 10;
static public function morning()
echo ‘Good Morning!!’;
static public function night()
echo “Good Night !!”;
public function show()
// morning(); // this cause an error
self::morning(); // use self keyword or parent keyword during the time you need to use the static class inside of the class

ollo::morning(); // before instantiating
ollo::$a++; // before instantiating
echo ollo::$a; // before instantiating

$object1 = new ollo(); // after instantiating
$object1->show(); // after instantiating

so , why and when we use static properties or method ..
First, they are available from anywhere in your script (assuming that
you have access to the class). This means you can access functionality without needing to pass an
instance of the class from object to object or, worse, storing an instance in a global variable. Second, a
static property is available to every instance of a class, so you can set values that you want to be available
to all members of a type. Finally, the fact that you don’t need an instance to access a


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s