Static Methods and Properties in PHP with Example

Static Methods and Properties in PHP with Example

In this post we will give you information about Static Methods and Properties in PHP with Example. Hear we will give you detail about Static Methods and Properties in PHP with ExampleAnd how to use it also give you demo for it if it is necessary.

Static Methods and Properties in PHP with Example

Static Methods and Properties is very useful feature in PHP and in this post i am going to tell you about Static Methods and Properties in PHP with example.

If you declare any class property or methods as static then you don’t need to create object of class to access that means it can be accessible without creating object of class. You can access directly from class with the help of scope resolution operator (::).

You can use static keyword to define static methods and properties.

If there is no visibility is defined then Static Methods and Properties in PHP will be treated as public.

I here write a simple example and understand you will be familiar with this syntax :

  1. $object=newmyClass();
  2. $object->variable1=2;
$object= new myClass();
$object->variable1=2;

You can see in above example, i change the public variable variable1 in class myClass but now i will give you example of static keywords in PHP 5. Now i will access the properties through the context of the class but make sure methods or properties must be declared static.


Example :

  1. class myClass {
  2. static public $variable1=5;
  3. static public $variable2=2;
  4. static public functiongetSum(){
  5.     $sum=self::$variable1+self::$variable2;
  6. print"Sum of two variables is ".$sum;
  7. }
  8. }
class myClass {
    static public $variable1 = 5;
    static public $variable2 = 2;
    static public function getSum() {
    	$sum=self::$variable1+self::$variable2;
        print "Sum of two variables is " . $sum;
    }
}

Now you will see in above example i have declared method and properties as static using static keyword.

To access the static method you will use class name with scope resolution operator(::).

  1. echo myClass::$variable1;
  2. myClass::getSum();
echo myClass::$variable1. "
"; myClass::getSum();

If you are trying to access regular property by static way then it will throw fatal error.

self keyword is used to access static property within the class.

If you extends the parent class in child class and want to access parent class property then you can use parent keyword.

  1. <!--?php
  2. class myParentClass
  3. {
  4.     public static$variable1=5;
  5. }
  6. class myClass extends myParentClass
  7. {
  8.     public static$variable2=2;
  9.     public $abc=2;
  10. functiongetSum()
  11. {
  12.     $sum=parent::$variable1+self::$variable2;
  13. print"Sum of two variables is ".$sum;
  14. }
  15. }
  16. echo myClass::$abc;//throw fatal error
  17. myClass::getSum();// give output Sum of two variables is 7
<!--?php
class myParentClass
{
	public static $variable1=5;
}
class myClass extends myParentClass
{
	public static $variable2=2;
	public $abc =2;
function getSum()
{
	$sum=parent::$variable1+self::$variable2;
    print "Sum of two variables is " . $sum;
}
}
echo myClass::$abc; //throw fatal error

myClass::getSum(); // give output Sum of two variables is 7


Make sure don’t use $this variable to call static methods.


Why use static methods and properties?

Now the question is why you will use static methods and properties in your application.

While working with large OOP based project you will have to use many classes and as i told you static methods aren’t associated with an instance so from any class you can access the properties of any other class by their class name and you won’t need to pass elements of different class manually through each class because static elements can be accessible from anywhere in your application.

Label :

PHP

OOP

Object Oriented Programming

Web Development

Hope this code and post will helped you for implement Static Methods and Properties in PHP with Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  31  =  40

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US