Advertisement
SamIsWicked

PHP OOP

Feb 22nd, 2025
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.73 KB | Source Code | 0 0
  1. <!-- Alright, here we are, PHP. PHP stands for Hypertext Preprocessor, it's a server-side back-end language that runs on the server, not the browser. -->
  2. <!-- You already know it, anyways, we have some demonstration of OOP in PHP.. -->
  3.  
  4. <!-- You are able to use PHP along with HTML without a problem. Just use opening tags <?php ?> and a question mark with > to end your PHP code, if you don't end it, then the rest of your file is PHP. -->
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8.     <meta charset="UTF-8">
  9.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10.     <title>OOP</title>
  11. </head>
  12. <body>
  13.    
  14. </body>
  15. </html>
  16.  
  17. <?php
  18. interface Vehicle { // Programming languages that support OOP and interfaces will have the same concept as this, but what is the concept?
  19.     // We first need to talk about what is OOP, OOP stands for "Object Oriented Programming", with OOP, we can create objects using classes or metatables in LUA, each object can have properties which are like variables and methods which are like funcitons.
  20.     // We are looking into PHP's implementation of OOP.
  21.     public function __construct($name, $color, $speed); // We will start with interfaces. When another class implements an interface class, they will be forced to use their own concepts of methods while also using all of the implemented interface's assigned methods.
  22.     // When we have an interface, we can't instantiate that interface as an object, but rather the other classes below that implement the interface, think of it like this:
  23.     // A vehicle is something that is used for travelling, each vehicle has their own way of travelling, like planes, cars, etc, so each class that implements from an interface must have their own unique way of driving.
  24.     // So what is a constructor? When I wrote __construct, it basically means what happens when we create an object, or creating a vehicle.
  25.     public function displayInfo(); // This is one of the methods that belongs to an object, we can make it belong to a class instead (like method called "displayVehicleCount"), so making it display the vehicle count using an object doesn't make sense, we can make it static to do that.
  26.  
  27.     public function drive(); // Same thing here.
  28. }
  29.  
  30. class Vehicle2 { // This is not an interface, this is just a normal class, it does not implement the methods and properties of an interface.
  31.     public $name; // We define each of the properties that a class would have, like the name of the vehicle, the color and speed.
  32.     public $color; // Make it "private" so that it can't regularly be accessible outside the class, but we can use getters and setters, although it's not necessary.
  33.     public $speed;
  34.  
  35.     public function __construct($name, $color, $speed) { // We are allowed to make a new vehicle unlike an interface. Though I wouldn't recommend it, that's why interfaces exist.
  36.         $this -> name = $name; // $this means the object, we can create more than 1 object, $this tells us we are accessing/setting an attribute that belongs to an object.
  37.         $this -> color = $color; // Now you might be wondering, what is that little arrow up there? This just tells it that the "color" or any other property belongs to the object, which is $this.
  38.         $this -> speed = $speed; // We can use the arrow for calling a function when we have an object, we will get to that soon.
  39.     }
  40.  
  41.     public function displayInfo() { // This is a method, it is something that belongs to the object.
  42.         echo "-- {$this -> name}'s Information --"; // echo is like printing, it outputs a string to the browser, it can also output HTML and CSS if we would like.
  43.         echo "<br>";
  44.         echo "Color: {$this -> color}"; // Now we are finally using $this.
  45.         echo "<br>";
  46.         echo "Speed: {$this -> speed}MPH";
  47.     }
  48.  
  49.     public function drive() {
  50.         echo "{$this -> name} is driving.";
  51.     }
  52. }
  53.  
  54. class Car implements Vehicle { // Here is an example of a class that implements from an interface.
  55.     public $name;
  56.     public $color;
  57.     public $speed;
  58.     static $amountOfCars = 0; // We are finally using static, it belongs to the class, not the object ($this). It makes more sense right?
  59.  
  60.     public function __construct($name, $color, $speed) { // One of the more logical reasons to use interfaces is that it forces a class that implements from it to write their own constructor.
  61.         $this -> name = $name;
  62.         $this -> color = $color;
  63.         $this -> speed = $speed;
  64.  
  65.         Car::$amountOfCars += 1; // So because of this, we can increase the amount of cars, since this is a static variable, we can access it from the class, not $this, we use :: to reference to a static variable or a constant in a class. Though that won't matter for now.
  66.     }
  67.  
  68.     public function displayInfo() { // When we implement, we must implement every function that's inside the interface that isn't static.
  69.         echo "-- {$this -> name}'s Information --";
  70.         echo "<br>";
  71.         echo "Color: {$this -> color}";
  72.         echo "<br>";
  73.         echo "Horsepower: {$this -> speed}MPH";
  74.     }
  75.  
  76.     public function drive() { // The example I mentioned in line 23, each vehicle drives in a different way, imagine this is a real world example.
  77.         echo "{$this -> name} is driving at {$this -> speed}MPH.";
  78.     }
  79. }
  80.  
  81. class Car2 extends Vehicle2 { // We are introduced to inheritance, because we put 'extends {CLASS_NAME}'
  82.     public $engine; // Since the parent class (the class we are inheriting from) already has the name and speed attribute defined, we can make more from here if we need to.
  83.     // This is considered a child class since we are inheriting from a parent class, which is Vehicle2.
  84.  
  85.     public function __construct($name, $color, $speed, $engine = "V8") { // A default property of a V8 engine, doesn't V8 seem familiar?
  86.         parent::__construct($name, $color, $speed); // We are calling the constructor of the parent class, we are giving it the required parameters.
  87.         $this -> engine = $engine;
  88.     }
  89. }
  90.  
  91. $toyota = new Car("Toyota", "Red", 120); // Now this is how to make a new object, we give it the necessary parameters, like we defined in the "Vehicle" class.
  92. echo "There are " . Car::$amountOfCars . " car(s).";
  93. echo "<br>"; // If you don't add a "br", it will all stay on the same line.
  94. echo $toyota -> name; // The name attribute belongs to the object, which is toyota, that's why we used the little arrow.
  95. echo "<br>";
  96. echo $toyota -> color;
  97. echo "<br>";
  98. $toyota -> drive();
  99. echo "<br>";
  100. $toyota -> displayInfo();
  101. echo "<br>";
  102. // I don't realy need to say more.
  103. $BMW = new Car2("BMW", "Blue", 135);
  104. echo $BMW -> name;
  105. echo "<br>";
  106. echo $BMW -> color;
  107. echo "<br>";
  108. $BMW -> drive();
  109. echo "<br>";
  110. $BMW -> displayInfo();
  111.  
  112. // Always use OOP, it saves time.
Tags: php OOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement