Verification: a143cc29221c9be0

Php class with static methods

Static Methods

Static methods can be called directly without needing an instance of the class (an object). And, $this pseudo-variable is not available inside static methods.

Static methods and properties can be considered as escaping from Object-Oriented Programming.

Static Methods - Declaration

The static keyword is used to declare static methods.

A visibility modifier may be added before the static keyword. If there's no visibility declaration, the visibility will be public. The same rules we learned in the visibility chapter are applied for each visibility.


class MyClass {
	public static function myStaticMethod() {
		echo "Hyvor!";
	}
}

Static Methods - Accessing

  • Static methods can be accessed from outside the class using the class name and Scope Resolution Operator (::). (Only if the visibility is public)

    
    MyClass::myStaticMethod();
    
    

    Example:

    PHP Static Methods Example

    
    

    Run Example ››

  • Static methods can be called from methods of other classes in the same way. To do this visibility of the static method should be public.
    
    class OtherClass {
    	public function myMethod() {
    		# visibility of myStaticMethod should be is public
    		MyClass::myStaticMethod();
    	}
    }
    
    
  • But, to access a static method from child classes of the class where the static method is declared, visibility of the static method can be protected.
    
    class OtherClass extends MyClass {
    	public function myMethod() {
    		# visibility of myStaticMethod should be is public or protected
    		MyClass::myStaticMethod();
    		# or
    		parent::myStaticMethod();
    	}
    }
    
    

    The parent keyword inside a child class refers to its parent class.

    Example:

    PHP Static Methods with Inheritance Example

    
     companyName = parent::getCompanyName();
    	}	
    }
    $hyvorAuth = new HyvorAuth;
    echo $hyvorAuth -> companyName;
    
    // ERROR will be thrown
    // echo Hyvor::getCompanyName();
    
    

    Run Example ››

    Note that we can call static methods from non-static methods (In the above example, we call Hyvor::getCompanyName() from HyvorAuth class's constructor method).
    And, as Hyvor::getCompanyName() is a protected method, you can only be accessed by its child classes.

  • Static methods can be accessed from the methods in the same class using the self keyword and ::.

    
    class MyClass {
    	public static function myStaticMethod() {
    		echo "Hyvor!";
    	}
    	public function myMethod() {
    		self::myStaticMethod();
    	}
    }
    
    

Static Methods - Example

PHP Static Methods Example


";
new MyClass();

Run Example ››

As you see in the above example, a class can have both static and non-static methods.

Static Properties

Static properties are properties that can be used without instantiating (or creating objects from classes).

Static Properties - Declaring


class MyClass {
	public static $myStaticProperty = 'Hyvor';	
}

Static Properties - Accessing

Accessing static properties are done in the same way as static methods. The same visibility rules apply.

Static Properties - Example

PHP Static Properties Example


';
MyClass::$name = 'Hyvor Developer';
new MyClass();

Run Example ››

Here's the explanation.

  • In the class, we declare a static property named $name and the constructor function.
  • Then, we echo the $name from outside the class. (Line break added for a handy output)
  • Then, we change the value of the $name static property. Changing the value of a static property is done in the same way as normal variables.
  • Finally, we create a new instance from MyClass. So, the constructor function will be called. We access the static $name from the constructor using the self keyword and ::

self:: is available when you call a non-static method. But, $this is not available when you call a static method.