PHP STATIC Keyword

we all know how a ststic keyword works in an object-oriented Programming Language like JAVA….we can also implement same concept in PHP too…

Let’s see how it works….

<?php

/*create a class of your choice…*/

class myclass

{

/* declare a static variable $name for example…*/

public static $name = “KRISHNA MANOJ”;

public static getName()

{

return self::$name;

/**self:: is to be used to indicate that $name  belongs to this class only…else we will not get any printed Output…**/

}

}

/* execute the function with object….*/

$obj = new myclass();

echo  $obj->getName();//works well as we are printing name from a function

/* with the classname itself….*/

echo myclass::getName();

?>

Leave a comment