Tag Archives: Class

Error Class

• Since php 7, many errors php reports now throw an error exception, not a fatal error as before.

• Subclasses of error exist for the specific type of error, such as parseerror or typeerror.

• To facilitate backwards compatibility, error is not derived from exception.

• Both error and exception implement the throwable interface

Object Cloning

Creating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to another object which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy.

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 cloned, PHP 5 will perform a shallow copy of all of the object’s properties. Any properties that are references to other variables will remain references.

Class Constants

It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don’t use the $ symbol to declare or use them. The default visibility of class constants is public.

The value must be a constant expression, not (for example) a variable, a property, or a function call.

It’s also possible for interfaces to have constants. Look at the interface documentation for examples.

As of PHP 5.3.0, it’s possible to reference the class using a variable. The variable’s value can not be a keyword (e.g. self, parent and static).

Note that class constants are allocated once per class, and not for each class instance.

  • Class constants are public
  • Class constants are being inherited
  • Class constants can be initialized by const
 class MyClass {
 const CONSTANT = 'constant value';

function showConstant() {
 echo self::CONSTANT . "\n";
 echo MyClass::CONSTANT . "\n";
 $class = new MyClass();

Final Class

Final keyword, which 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.

  • A final class may be instantiated.
  • A class with a final function may be derived.
  • Static functions can be final.
  • Preventing massive inheritance chain of doom
  • Encouraging composition
  • Force the developer to think about user public API
  • Force the developer to shrink an object’s public API
  • A final class can always be made extensible
  • extends breaks encapsulation
  • You don’t need that flexibility
  • You are free to change the code


The SplFixedArray class provides the main functionalities of array. The main differences between a SplFixedArray and a normal PHP array is that the SplFixedArray is of fixed length and allows only integers within the range as indexes. The advantage is that it allows a faster array implementation.


A Trait is simply a group of methods that you want include within another class. A Trait, like an abstract class, cannot be instantiated on it’s own.

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

A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a Trait on its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

  • Multiple traits can be used by a single class.
  • A trait can declare a private variable.
  • Traits are able to be auto-loaded.
 trait A {
 public function hello() {
 return "PHP";
 public function world() {
 return "Codez";
 class test{
 use A ;
 $obj = new test();
 echo $obj->hello()."\n";
 echo $obj->world()."\n";


stdClass is Generic empty class in PHP OR stdClass is universal base class in PHP.

stdClass is used to create anonymous objects with properties.

When we need to create an object without having new class. then we used stdClass class which is inbuilt in PHP.
After create object, we can add properties.

$object=new stdClass();
echo $object->name;