Advertisement
ExeQue

Untitled

Dec 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.88 KB | None | 0 0
  1. <?php
  2. class User {
  3.     private $name; // Class Member Property
  4.    
  5.     private static $users = array( // Class Property
  6.         1=>array(
  7.             "fname" => "Ted",
  8.             "lname"->"Mosby"),
  9.         2=>array(
  10.             "fname" => "Robin",
  11.             "lname"->"Scherbatsky")
  12.         );
  13.    
  14.     public function GetName() { // Class Member Method
  15.         return $this->name;
  16.     }
  17.    
  18.     public static function GetFirstName($id) { // Class Method
  19.         return self::$users[$id]["fname"];
  20.     }
  21.    
  22.     public function __construct($name) { // Constructor
  23.         $this->name = $name;
  24.     }
  25. }
  26.  
  27. To access a class member method, a new instance of the class has to be created:
  28.  
  29. $user = new User("Robin"); // <- Class member - Is an instance of the class User
  30.  
  31. echo $user->GetName(); // Call of a class member method
  32.  
  33. /* prints: Robin */
  34.  
  35. To access a class method, a reference to the class has to be used:
  36.  
  37. var_dump(User::GetFirstName(1));
  38.  
  39. /* prints: Ted */
  40.  
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement