Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il

Hostgator - Save 50% Off Your New Optimized WordPress Hosting With Coupon: WDA50

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Dedicated Hosting Superb Internet

simplilearn big data and analytics

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Buy Cool Shirts

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


6,300 questions

8,938 answers

573 users

How to use class static methods in PHP

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial


17 views
asked Nov 4, 2015 by avibootz
edited Nov 5, 2015 by avibootz

2 Answers

0 votes
class Test 
{
    public $pr = "Class property from class Test";
    public static $i = 0;
    
    public function __construct()
    {
        echo 'Constructor activated from class Test <br />';
    }
      
    public function __destruct()
    {
        echo '<br /> Destructor activated from class Test <br />';
    }
    
    protected function setProperty($val)
    {
        $this->pr = $val;
    }
    
    protected function getProperty()
    {
        return $this->pr;
    }
    public static function f()
    {
        return "i = " . ++self::$i . " <br />";
    }
}
  
class MyNewClass extends Test
{
    public function __construct()
    {
        parent::__construct(); 
        echo 'Constructor activated from class MyNewClass <br />';
    }

}

/* 
 static method can be accessed without first instantiating the class Test
 static values their stored values until the program end
 static method need to use static values  
*/
 
do
{
  // Call method f() without instantiating class Test
  echo Test::f();
} while ( Test::$i < 5 );







/*
run:

i = 1 
i = 2 
i = 3 
i = 4 
i = 5 

*/

 




answered Nov 4, 2015 by avibootz
0 votes
class Dog {
    public static function whatYouSay() {
         echo 'Woof! Woof!';
    }

    public static function speak() {
         self::whatYouSay();
    }
}

Dog::speak(); 

 
/*
run: 
  
Woof! Woof!
  
*/ 

 




answered Sep 7 by avibootz
...