Created
March 21, 2021 17:57
-
-
Save farithadnan/c9a12fc4a954f245d6353c088a73fc53 to your computer and use it in GitHub Desktop.
PHP OOP fundamental | Quick Refresh
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 | |
| // constructor is use to make PHP run the function automatically | |
| // without instantied the function of a class. | |
| class Cars { | |
| public $wheel_count = 4; | |
| static $door_count = 4; | |
| // this is how to use constructor | |
| function __construct(){ | |
| echo $this->wheel_count; | |
| // this will increment everytime the class is invoke | |
| // it will store the incrementing value in this statement | |
| echo self::$door_count++; | |
| } | |
| // this is how to use deconstructor | |
| // it will do the opposite from the constructor | |
| function __destruct(){ | |
| echo "<br/>"; | |
| echo "destructor: "; | |
| echo $this->wheel_count; | |
| echo self::$door_count--; | |
| } | |
| } | |
| $bmw = new Cars(); | |
| echo "<br/>"; | |
| $mercedes = new Cars(); | |
| echo "<br/>"; | |
| $mercedes2 = new Cars(); | |
| echo "<br/>"; | |
| // We dont use below statement for constructor | |
| // echo $bmw->display(); | |
| ?> |
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 | |
| // this is how to define the class using php oop | |
| class Cars { | |
| } | |
| // get_declared_classes(): is where PHP will get all exisiting class automatically, including the class above | |
| $my_classes = get_declared_classes(); | |
| // this will preview all the classes that exist | |
| foreach ($my_classes as $class) { | |
| echo $class . "<br>"; | |
| } | |
| ?> |
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 | |
| // We defining function in a class as a method, and define a function when it's outside the class | |
| // class is like a blueprint for an object | |
| class Cars { | |
| // function is a behavioral/action that can be done by the object | |
| function greeting(){ | |
| } | |
| function greeting2(){ | |
| } | |
| } | |
| // get_class_methods: will get all the method inside class Cars. | |
| $the_methods = get_class_methods('Cars'); | |
| foreach ($the_methods as $method) { | |
| echo $method . "<br>"; | |
| } | |
| ?> |
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 | |
| class Cars { | |
| function greeting(){ | |
| echo "Hello Student <br/>"; | |
| } | |
| } | |
| // Imagine classes is like a template/blueprint, for a cars for example, | |
| // there's many cars out there right? so how can we should do if there's more than | |
| // one cars that have a same behavioural/characteristic, solution is to use instances | |
| // we instantiate a different type of car by assigning the blueprint to it. | |
| // Same template but difference instances. | |
| $bmw = new Cars(); | |
| $mercedes = new Cars(); | |
| // below is how to use the method in a class for specific instances. | |
| $bmw->greeting(); | |
| $mercedes->greeting(); | |
| ?> |
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 | |
| // some people called it properties, some people called it class's variable | |
| class Cars { | |
| // we need to use var if we need to let php know we declaring a variable in a class | |
| var $wheel_count = 4; | |
| var $door_count = 4; | |
| function car_detail(){ | |
| // using pseudo variable called $this to refer this specific class, and be able to access the property contain inside it | |
| return "This car has " . $this->wheel_count . " Wheels"; | |
| } | |
| } | |
| $bmw = new Cars(); | |
| $mercedes = new Cars(); | |
| // We don't declaring the dollar sign if we need to use the variable bcs we already declare it in the class above | |
| echo $bmw->wheel_count; | |
| echo "<br/>"; | |
| // We can also assign a new value directly like example below | |
| echo $mercedes->wheel_count = 10; | |
| echo "<br/>"; | |
| echo $mercedes->car_detail(); | |
| ?> |
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 | |
| // Inheritence is like inheriting your riches to your kids. In object/classes, other classes can uses other classes property or | |
| // method by using extends. | |
| class Cars { | |
| var $wheels = 4; | |
| var $doors = 4; | |
| function greeting(){ | |
| return "Hello"; | |
| } | |
| } | |
| $bmw = new Cars(); | |
| // Belows is how the other classes can use other classes property or value by using extends | |
| class Trucks extends Cars { | |
| var $wheels = 10; | |
| } | |
| $tacoma = new Trucks(); | |
| // this will let this instances using other classes | |
| echo $tacoma->doors; | |
| echo "<br/>"; | |
| // this will replace the extend property and uses its own property bcs of the same variable name | |
| echo $tacoma->wheels; | |
| echo "<br/>"; | |
| // This will return the greeting function, this proof that trucks able t inherit cars property and methods | |
| echo $tacoma->greeting(); | |
| ?> |
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 | |
| // Access control modifiers is use to control the flow of our program, this modifiers consist: | |
| // public, protected, private | |
| // Instead of using var it's needed to use the modifier like public, protected, private | |
| class Cars { | |
| // Public means that it can be used for the entire program | |
| public $wheel_count = 4; | |
| // Prviate means it can only be use in this classes only | |
| private $door_count = 4; | |
| // Protected means it only avalable in its classs and its sub-classes | |
| protected $seat_count = 2; | |
| function car_detail(){ | |
| echo $this->wheel_count; | |
| echo "<br/>"; | |
| echo $this->door_count; | |
| echo "<br/>"; | |
| echo $this->seat_count; | |
| } | |
| } | |
| $bmw = new Cars(); | |
| echo $bmw->wheel_count; | |
| echo " is a Public Variable <br/>"; | |
| // Below will not work bcs of the modifier that been set | |
| // echo $bmw->door_count; | |
| // echo $bmw->seat_count; | |
| // Below shows that all the data can be extract thru function, even tho it has been assign with the access modifier | |
| // it will not work if we try to call the variable directly from the classes bcs of the access modifier | |
| $bmw->car_detail(); | |
| ?> |
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 | |
| // Different between static property/function and the usual property/function | |
| // is that you don't have to instantied the class to becoming and object | |
| // we can just directly call the property/function to use them | |
| class Cars { | |
| static $wheel_count = 4; | |
| // Below is how we declare the static property | |
| static $door_count = 4; | |
| static function car_detail(){ | |
| // when we using static we dont need the $this anymore | |
| // echo $this->wheel_count; | |
| // echo $this->door_count; | |
| // echo $this->seat_count; | |
| echo Cars::$wheel_count; | |
| echo Cars::$door_count; | |
| } | |
| } | |
| // $bmw = new Cars(); | |
| // echo $bmw->wheel_count; | |
| // this will not work bcs we have created door_count as a static property we can't use this | |
| // type of invocation that require a instant. | |
| // echo $bmw->door_count; | |
| // this is the right invocation when dealing with a static property/function, :: -> static | |
| echo Cars::$door_count; | |
| Cars::car_detail(); | |
| ?> |
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 | |
| class Cars { | |
| // setters & getters is only applicable when dealing with private property | |
| // so that we can access private property and able to control the program alot better | |
| private $door_count = 4; | |
| // getters mean getting up data | |
| function get_values(){ | |
| echo $this->door_count; | |
| } | |
| // setters mean setting up data | |
| function set_values(){ | |
| $this->door_count = 10; | |
| } | |
| } | |
| $bmw = new Cars(); | |
| $bmw->set_values(); | |
| $bmw->get_values(); | |
| ?> |
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 | |
| // This is an additon to the class 7 about static modifier, the different is that | |
| // instead of using class::$property now we use self::$property | |
| // the outcome is different but there's a reason why it's need and when to use SELF | |
| // Declaration of the first class that has static property and static function | |
| // where it returning a property value of wheel_count | |
| class Cars { | |
| static $wheel_count = 4; | |
| static function car_detail() { | |
| // instead of using Class:: we need to declare this way if we need to use self:: | |
| return self::$wheel_count; | |
| } | |
| } | |
| // This is a cars subclasses where it has static function display() which invoks car_Detail() function | |
| // from the parent class. after invoking parent function it will now receive the return value from | |
| // the parent class and will print out the outcome | |
| class Trucks extends Cars { | |
| static function display() { | |
| // parent::refer to parent of this class or the class that it has extends to | |
| echo parent::car_detail(); | |
| } | |
| } | |
| // when dealing with static we don't need to instantied the class to a variable | |
| // only specific the name of the class and call the static property/function | |
| Trucks::display(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment