Showing posts with label static properties. Show all posts
Showing posts with label static properties. Show all posts

Monday, 17 May 2021

Php: Access static properties from super classes

In Php, static properties and methods are inherited. If you update the value of a static property same will be reflected to all the subclasses and this class.

 

static_data_inheritance.php

#!/usr/bin/php

<?php

    class ParentClass{
        public static $name = 'Parent class';
    }

    class ChildClass1 extends ParentClass{

    }

    class ChildClass2 extends ParentClass{

    }

    class ChildClass3 extends ChildClass2{

    }

    function print_name(){
        $name1 = ParentClass::$name;
        $name2 = ChildClass1::$name;
        $name3 = ChildClass2::$name;
        $name4 = ChildClass3::$name;

        echo "\nParentClass : $name1\n";
        echo "ChildClass1 : $name1\n";
        echo "ChildClass2 : $name1\n";
        echo "ChildClass3 : $name1\n";
    }
   
    print_name();

    echo "\nUpdating name using ChildClass1\n";
    ChildClass1::$name = "Child class1";

    print_name();

?>

 

Output

$./static_data_inheritance.php 


ParentClass : Parent class
ChildClass1 : Parent class
ChildClass2 : Parent class
ChildClass3 : Parent class

Updating name using ChildClass1

ParentClass : Child class1
ChildClass1 : Child class1
ChildClass2 : Child class1
ChildClass3 : Child class1

 

  

Previous                                                    Next                                                    Home

Sunday, 16 May 2021

Php: Static properties

 

Static properties are associated with class not with an object.

 

How to define a static property?

Using ‘static’ keyword you can define a static property.

 

Syntax

static $variable_name;

 

Example

static $organization_name = 'ABC Corp';

static $total_employees = 100;

 

How to access static properties?

Syntax

ClassName::$name_of_the_property;

 

Example

Employee::$organization_name;

 

How to access the static properties inside the class?

Syntax

self::$name_of_the_property

 

Example

$org_name = self::$organization_name;

$total_emps = self::$total_employees;

 

Can I apply access specifiers/visibility modifiers to static properties?

Yes, you can do.

 

Example

public static $organization_name = 'ABC Corp';

private static $total_employees = 100;

 

Find the below working application.

 

static_properties_demo.php

#!/usr/bin/php

<?php

    class Employee{
        public static $organization_name = 'ABC Corp';
        private static $total_employees = 100;

        public static function about_class(){
            $org_name = self::$organization_name;
            $total_emps = self::$total_employees;

            echo "organization name : $org_name\n";
            echo "total employees : $total_emps\n";
        }

    }

    $org_name = Employee::$organization_name;
    //since total_employees is private, this will not run
    //$total_emp = Employee::$total_employees; 
   
    Employee::about_class();
   
?>

Output

$./static_properties_demo.php 

organization name : ABC Corp
total employees : 100

Note

a.   Static properties are not accessible via instance.

b.   You can’t access $this from static method.

c.    Private static properties are not accessible outside of the class.



Previous                                                    Next                                                    Home