Properties of a PHP Class

Class member variables are called “properties”.They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration.In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in … Read more

PHP 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.

CONSTANT
<?php

class MyClass
{
const CONSTANT = ‘Kiran’;

function showConstant() {
echo self::CONSTANT . “\n”;

Read more

Autoloading Classes in PHP

Define an __autoload() function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

__autoload
<?php

function __autoload($class_name) {
include

Read more

PHP Constructors and Destructors

Constructors : PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

__construct
<?php

class TestClass {
function __construct() {
print

Read more

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