LAMP Articles

self:: vs static:: in PHP

0 👍
👎 1
 PHP
 LAMP

self:: vs static:: in PHP is all about inheritance and late static binding.  It’s used when you are dealing with overrides and methods coming from the parent class.

 

self::

 - Refers to the class where the method is defined.

 - Does not consider inheritance overrides.

static::

 - Refers to the class that is actually called at runtime.

 - This is called late static binding.
- Allows child classes to override static properties/methods and still be respected.

 

 class Automobile {

    public static $type = "Automobile";

 

    public static function getTypeUsingSelf() {

        return self::$type// bound to Automobile

    }

 

    public static function getTypeUsingStatic() {

        return static::$type; // late static binding

    }

 }

 

 class Car extends Automobile {

    public static $type = "Car";

 }

 

 class Truck extends Automobile {

    public static $type = "Truck";

 }

 

 class Van extends Automobile {

    public static $type = "Van";

 }

 

 // ------------------- USAGE -------------------

 

 echo Car::getTypeUsingSelf();   // Output: Automobile

 echo "<br>";

 echo Car::getTypeUsingStatic(); // Output: Car

 

 

PHP Access Modifiers (public, private, protected)

0 👍
👎 0
 PHP
 LAMP

In PHP Object-Oriented programming, public, private, and protected are access modifiers (visibility keywords) used to control how properties (variables) and methods (functions) of a class can be accessed.

They are important in Object-Oriented Programming (OOP) because they enforce encapsulation (controlling how data is exposed and used).


Public
Members declared public can be accessed from inside the class, outside of the class and by subclasses (child classes).  If no visibility is specified on a property the default will be public.


class
Car {

    public $brand;

    public function setBrand($brand) {

   $this->brand = $brand; // accessible inside the class

    }

 }

 $car = new Car();

 $car->brand = "Toyota"; // accessible outside the class

echo $car->brand; // Output: Toyota


Private

Members declared private can only be accessed inside of the class that defines them.  They cannot be accessed from outside the class or by subclasses.

 


class Car {

    private $engineNumber;

    public function setEngineNumber($num) {

       $this->engineNumber = $num; // accessible inside the class

    }

 

    public function getEngineNumber() {

       return $this->engineNumber; // allowed via public method

    }

 }

 

 $car = new Car();

 $car->setEngineNumber("ENG123");

 // echo $car->engineNumber; ERROR: Cannot access private property

 echo $car->getEngineNumber(); // Output: ENG123

 

 

Protected

Members declared protected can be accessed inside of the class, in child(sub) classes that inherit from it.  They cannot be accessed directly from outside of the class… only subclasses.

 

class Vehicle {

  protected $type = "Generic Vehicle";

 

   protected function getType() {

       return $this->type;

   }

}

 

class Car extends Vehicle {

  public function showType() {

       return $this->getType(); // accessible in child class

   }

}

 

$car = new Car();

// echo $car->type; ERROR: Cannot access protected property

echo $car->showType(); // Output: Generic Vehicle

 

 

PHP Object Oriented Class Inheritance

0 👍
👎 0
 PHP
 LAMP

PHP class inheritance is a mechanism that allows one class (called the child class or subclass) to inherit properties and methods from another class (called the parent class or superclass).

 

This allows you to reuse code, extend existing functionality, and follow the principle of "Don’t Repeat Yourself (DRY)".


Basics of Inheritance

1. The extends keyword is used in PHP to create inheritance.

2. A child class automatically inherits all the public and protected properties and methods of its parent class.

3. The child class can:

       A. Use the inherited methods and properties directly.

       B. Override methods from the parent class.

       C. Add new methods and properties.

 

Example 1: Basic Inheritance

Here, the Dog class inherits from the Animal class but overrides the speak() method.

 

 // Parent class

 class Animal {

    public $name;

 

    public function __construct($name) {

        $this->name = $name;

    }

 

    public function speak() {

        echo "$this->name makes a sound.<br>";

    }

 }

 

 // Child class

 class Dog extends Animal {

    public function speak() {

        echo "$this->name barks! Woof Woof!<br>";

    }

 }

 

 // Usage

 $animal = new Animal("Generic Animal");

 $animal->speak(); // Output: Generic Animal makes a sound.

 

 $dog = new Dog("Buddy");

 $dog->speak(); // Output: Buddy barks! Woof Woof!

 

 

Example 2: Using parent:: to Call Parent Methods

The Car class overrides the start() method but still uses the parent’s method with parent::start().

 

 class Vehicle {

    public function start() {

        echo "Starting the vehicle...<br>";

    }

 }

 

 class Car extends Vehicle {

    public function start() {

        // Call the parent method first

        parent::start();

        echo "Car engine started!<br>";

    }

 }

 

 $car = new Car();

 $car->start();

 // Output:

 // Starting the vehicle...

 // Car engine started!

 

 

Example 3: Multilevel Inheritance

Human inherits from Animal, and Animal inherits from LivingBeing. So, Human has all methods from both parents.

 

 class LivingBeing {

    public function breathe() {

        echo "Breathing...<br>";

    }

 }

 

 class Animal extends LivingBeing {

    public function eat() {

        echo "Eating...<br>";

    }

 }

 

 class Human extends Animal {

    public function speak() {

        echo "Speaking...<br>";

    }

 }

 

 $person = new Human();

 $person->breathe(); // From LivingBeing

 $person->eat();     // From Animal

 $person->speak();   // From Human

 

 

A Quick HowTo of PHP Operators

0 👍
👎 0
 LAMP

Math Operators

Addition:
Caculate the sum of two numbers using the + operator.

$sum = 1 + 3;
The value of $sum is 4.

Subtraction:
Caculate the difference of two numbers using the - operator.

$diff = 3 - 2;
The value of $diff is 1.

Division:
Calculate the quotient of two numbers using the / operator.

$quotient = 4 / 2;
The value of $quotient is 2.

Multiplication:
Calculate the product using the * operator.

$product = 4 * 5;
The value of $product is 20.

Modulo:
Gives the remainder after dividing two numbers the % operator.

$remainder = 10 % 3;
The value of $remainder is 1.

PHP Null Coalescing Operator ??

0 👍
👎 0
 LAMP
The null coalescing operator ?? is used similar to the elvis operator ?:  In the example below the value of $var3 will only be the value of $var2 if $var1 is null.  With no value will $var3 be the value of $var1. 
 
Example 1:

$var1 = null;  // it's null
$var2 = 15;
$var3 = $var1 ?? $var2; // the value is 15

OR
$var1;  // it's undfefined
$var2 = 15;
$var3 = $var1 ?? $var2; // the value is 15

In Example 1 above the value of $var3 will be the value of $var2.  This is because value of $var1 is null or undefined.  If $var1 is any value besides null, the value of $var3 would be the value of $var1.  That case is displayed in Example 2 below.

In Example 2:

$var1 = 3;
$var2 = 15;
$var3 = $var1 ?? $var2;   // the value is 3