Object Inheritance in PHP

Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. It will affect the way many classes and objects relate to one another.

For example, when you extend a PHP class, the subclass inherits all of the public and protected methods from the … Read more

Static Keyword in PHP

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static , then it can not be accessed with an instantiated class object (though a static method can).

static
<?php

class TestClass
{
public static $my_static = ‘Test01’;

public

Read more

PHP Class Abstraction

An abstract class is a class that contains at least one abstract method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as “abstract”.
The purpose of this is to provide a kind of template to inherit from … Read more

Object Interfaces in PHP

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.

It defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.

interface
<?php
Read more

Traits in PHP

As of PHP 5.4.0, PHP implements a method of code reuse called Traits.

This Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in … Read more

Magic Methods in PHP

The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state() and __clone() are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.

PHP reserves all function names starting … Read more

Final Keyword in PHP class

PHP 5 introduces the final keyword. It prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Final methods example
<?php

class BaseClass {
public function test() {
echo “BaseClass::test() called\n”;
}

final public

Read more

Object Cloning in PHP

Creating a copy of an object with fully replicated properties is not always the wanted behavior.

An object copy is created by using the clone keyword (which calls the object’s __clone() method if possible). An object’s __clone() method cannot be called directly.

$copy_of_object = clone $object;

When an object is … Read more