Advertisement
codingbutter

Static Example

Oct 1st, 2022
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.40 KB | None | 0 0
  1. <?php
  2. class Person{
  3.     public static $total_people = 0;
  4.     private $name = "";
  5.     function __construct($name){
  6.         $this->name = $name;
  7.         self::$total_people++;
  8.     }
  9.     public function sayName(){
  10.         print($this->name."\n");
  11.     }
  12.     public static function peopleCount(){
  13.         print(self::$total_people."\n");
  14.     }
  15. }
  16.  
  17. $john = new Person("John");
  18. $john->sayName(); //prints john
  19. Person::peopleCount(); //prints 1
  20. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement