Last active
August 8, 2019 20:47
-
-
Save cesarkohl/1b971f1c6c10f1c86e76cca10c52bd24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| ini_set('display_errors', 1); | |
| ini_set('display_startup_errors', 1); | |
| error_reporting(E_ALL); | |
| class Father { | |
| // Property | |
| public $lastName; | |
| // Method | |
| public function getLastName() { | |
| return $this->lastName; | |
| } | |
| } | |
| ////////////////////////////////////// | |
| class Father { | |
| } | |
| class Son extends Father { | |
| } | |
| //////////////////////////////////////// | |
| class Father { | |
| protected $lastName = 'OOP'; | |
| public function getLastName() { | |
| return $this->lastName; | |
| } | |
| } | |
| class Son extends Father { | |
| public function getLastName() { | |
| return 'Not ' . $this->lastName; | |
| } | |
| } | |
| $father = new Father(); | |
| echo $father->getLastName(); // return OOP | |
| $son = new Son(); | |
| echo $son->getLastName(); // return Not OOP | |
| //////////////////////////////////////// | |
| abstract class Person { | |
| } | |
| class Father extends Person { | |
| } | |
| final class Son extends Father { | |
| } | |
| class Grandson extends Son { | |
| } | |
| $person = new Person(); // return Cannot instantiate class Person | |
| $father = new Father(); | |
| $son = new Son(); | |
| $grandson = new Grandson(); // Cannot exist. To declare an extension | |
| // of a final class kills the execution | |
| //////////////////////////////////////// | |
| class Father { | |
| public $public; | |
| protected $protected; | |
| private $private; | |
| } | |
| //////////////////////////////////////// | |
| class Numbers { | |
| const pi = 3.14; | |
| public function getPi() { | |
| return self::pi; | |
| } | |
| } | |
| $numbers = new Numbers(); | |
| echo $numbers->getPi(); // return 3.14 | |
| //////////////////////////////////////// | |
| class Numbers { | |
| const pi = 3.14; | |
| public function getPi() { | |
| return self::pi; | |
| } | |
| } | |
| class App extends Numbers { | |
| const version = 1.0; | |
| public function getItAll() { | |
| echo 'App version: ' . self::version; | |
| echo 'Pi number: ' . parent::pi; | |
| } | |
| } | |
| $app = new App(); | |
| echo $app->getPi(); // return 3.14 | |
| echo $app->getItAll(); // return App version: 1.0 Pi number: 3.14 | |
| //////////////////////////////////////// | |
| class Statics { | |
| public const staticConst = 'Static Const'; | |
| public static $staticProperty = 'Static Property'; | |
| public static function staticMethod() { | |
| return 'Static Method'; | |
| } | |
| } | |
| echo Statics::staticConst; // return Static Const | |
| echo Statics::$staticProperty; // return Static Property | |
| echo Statics::staticMethod(); // return Static Method | |
| //////////////////////////////////////// | |
| class WineHouse { | |
| public $name = 'Wine House'; | |
| } | |
| class Wine { | |
| public $wineHouse; | |
| public function __construct($wineHouse) { | |
| $this->wineHouse = $wineHouse; | |
| } | |
| } | |
| $wineHouse = new WineHouse(); | |
| $wine = new Wine($wineHouse); | |
| echo $wine->wineHouse->name; // return Wine House | |
| ////////////////////////////////////// | |
| class Product { | |
| public $name; | |
| public function __construct($name) { | |
| $this->name = $name; | |
| } | |
| } | |
| class Cart { | |
| public $products; | |
| public function addProduct(Product $product) { | |
| $this->products[] = $product; | |
| } | |
| public function getAllProducts() { | |
| return $this->products; | |
| } | |
| } | |
| $productA = new Product('Product A'); | |
| $productB = new Product('Product B'); | |
| $productC = new Product('Product C'); | |
| $cart = new Cart(); | |
| $cart->addProduct($productA); | |
| $cart->addProduct($productB); | |
| $cart->addProduct($productC); | |
| print_r($cart->getAllProducts()); | |
| /* | |
| return Array ( | |
| [0] => Product Object ( [name] => Product A ) | |
| [1] => Product Object ( [name] => Product B ) | |
| [2] => Product Object ( [name] => Product C ) | |
| ) | |
| */ | |
| ////////////////////////////////////// | |
| class Contact { | |
| public $name = 'Contact'; | |
| } | |
| class Client { | |
| public $contact; | |
| public function __construct() { | |
| $this->contact = new Contact(); | |
| } | |
| } | |
| $client = new Client(); | |
| echo $client->contact->name; | |
| //////////////////////////////////////// | |
| class Person { | |
| private $age; | |
| public function __set($property, $value) { | |
| $this->property = $value; | |
| echo $property . ' = ' . $value; | |
| } | |
| public function __get($property) { | |
| echo 'This property is private/protected: ' . $property; | |
| } | |
| public function __call($method, $parameters) { | |
| echo 'The method '. $method . ' does not exist.'; | |
| } | |
| } | |
| $person = new Person(); | |
| $person->age = 30; // return age = 30 | |
| echo $person->age; // return This property is private/protected: age | |
| $person->setFavoriteMovie('Lion King'); // return The method setFavoriteMovie does not exist | |
| ////////////////////////////////////// | |
| require_once('ClassA.php'); | |
| require_once('ClassB.php'); | |
| require_once('ClassC.php'); | |
| function __autoload($class) { | |
| require_once($class . '.php'); | |
| } | |
| $objectA = new ClassA(); | |
| $objectB = new ClassB(); | |
| $objectC = new ClassC(); | |
| echo $objectA->name; | |
| echo $objectB->name; | |
| echo $objectC->name; | |
| ////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment