Static Members in PHP OOPS

<?php

//  Example 1
class Abc {
    public static $data = "test data";
    public static $size = "big data here";
    public static function xyz() {
        echo "xyz function <br>";
    }

    public static function getSize() {
        return self::$size;
    }

    public static function setSize($s) {
        return self::$size = $s;
    }
}
echo Abc::$data . "<br>";
Abc::xyz() . "<br>";
echo Abc::getSize() . "<br>";
echo Abc::setSize(1200) . "<br><br><br>";

//  Example 2
class AbcTwo {
    public static $objectCount = 0;
   
    public static function getCount() {
        return "Total Objects created by class are : " . self::$objectCount;
        echo "Hello this is getCount function called <br>";
    }
   
    public function __construct() {
        self::$objectCount++;
    }
}
$a = new AbcTwo();
$b = new AbcTwo();
$c = new AbcTwo();
$d = new AbcTwo();
$e = new AbcTwo();

echo AbcTwo::getCount();

// OUTPUT :
//test data
// xyz function
// big data here
// 1200


// Total Objects created by class are : 5

?> 

Comments

Popular posts from this blog

Ajax Insert Records in Database Tutorial in PHP

Displaying Database data using Class, Object, Constructor, Destructor in PHP MySql

SubQuery with EXISTS and NOT EXISTS in PHP