Last Updated:

Object-oriented programming in PHP

Classes contain class members that represent properties and methodsClass properties are variables, and class methods are functions that interact with properties.

Properties (variables) are declared using the service word var and the variable name $per , and methods (functions) are written as simple functions.

The class is accessed by declaring an object using the service word new and the name of the class.

class Gena
{
var $per='Property (variable) of the Gena class<br>';
function f1()
{
echo 'Method (function) of the Gena class';
}
}
$obj=new Gena;
echo $obj->per; Property (variable) of the Gena
$obj->f1() class; Gena class method (function)

public - You can access a type or member from any other code in the same assembly or other assembly that references it.

private - A type or member can only be accessed from code in the same class or structure.

protected - A type or member can only be accessed from code in the same class or structure, or in a derived class.

internal - You can access a type or member from any code in the same assembly, but not from another assembly.

All properties (Variables) by default public (var - deprecated)

All default methods (Functions) public and static

Encapsulation

Encapsulation is a mechanism that combines data and processes its code as a whole.

Encapsulation is the inclusion of various small elements in a larger object, as a result of which the programmer works directly with this object.

class Gena
{
private $a='Gena';
function f1()
{
return $this->a;
}
}

$obj=new Gena;
//echo $obj->a; MUST NOT!!!
echo $obj->f1(); Gene

OOP - hides unnecessary codes from the programmer, which allows him to focus on solving common problems. In OOP, this capability is provided by classes, objects, and various means of expressing hierarchical relationships between them.

Inheritance

Inheritance allows one object to acquire the properties of another object, not to be confused with copying objects. When you copy, an exact copy of an object is created, and when you inherit, an exact copy is supplemented with unique properties that are unique to the derived object.

class Gena
{
public $a='Gena';
function f1($b)
{
return"$b, $this->a";
}
}

class Gena2 extends Gena
{

}
$obj=new Gena2;
$obj->a='Vova'; Change the echo property
$obj->f1('Hello'); Hello, Vova

or

class Gena1
{
public $a='Gena';
function f1($b)
{
return"$b, $this->a";
}
}

class Gena2 extends Gena1
{

}

class Gena3 extends Gena2
{

}

$obj=new Gena3;
$obj->a='Vova'; Change the echo property
$obj->f1('Hello'); Hello, Vova

Polymorphism

Polymorphism in php allows you to use the same names for similar but technically different tasks. The main thing about polymorphism is that it allows you to manipulate objects by creating standard interfaces for similar actions. Polymorphism makes it much easier to write complex programs.

class A 
{
function Test() // Displays which class function was called
{
echo "class A - Test <br>\n";
}

function Call() // Test function — simply redirects to Test()
{
$this->Test();
}
}

class B extends A
{
function Test() // Test() function for class B
{
echo "class B - Test <br>\n";
}
}

$a=new A();
$b=new B();

$a->Test(); class A - Test
$b->Test(); class B - Test
$b->Call(); class B - Test Note!!!

Properties and methods within the class are accessed through $this-> (dashes and greater).

class Gena
{
var $per='Property Gena<br>';
function f1()
{
return'Function and '.$this->per;
}
}

$obj=new Gena;
echo $obj->f1(); Gena Function and Property

Sometimes you want to automatically initialize the data immediately when you access the class (when you create an object), you use constructors to do this. Constructor is an auto-start function that has the same name as the class or reserved name __construct()

class Gena
{
function __construct()
{
echo 'Auto start';
}
}

$obj=new Gena; Auto Start

You can pass an argument to the constructor when you create an object.

class Gena
{
function Gena($a)
{
echo 'Auto start + '.$a;
}
}
$obj=new Gena('Argument'); Auto Start + Argument

Constructors in parent classes are not automatically called. To call the constructor declared in the parent class, refer to the parent::__construct() method.

class Gena 
{
function __construct()
{
echo "Gena class constructor<br>\n";
}
}
class Roma extends Gena
{
function __construct()
{
parent::__construct();
echo "Roma<br class constructor>\n";
}
}

$obj=new Gena(); Constructor of the Gena
class$obj=new Roma(); Gena Class Constructor Roma Class Constructor

If PHP 5 cannot find the declared __construct() method, the constructor will be called according to the previous schema, through a call to a method whose name corresponds to the name of the class. There can be only one compatibility problem with old code if it contains classes with __construct() methods.

PHP 5 provides a destructor concept similar to those used in other OO languages, such as Java: when the last reference to an object is released, the __destruct() method is called before releasing the memory occupied by that object, which does not accept parameters.

class Gena 
{
function __construct()
{
echo "Constructor<br>\n";
$this->name="<b>all</b>";
}

function __destruct()
{
echo 'destroyed '.$this->name.' <br>\n";
}
}

$obj=new Gena(); The constructor is destroyed everything

As with constructors, destructors declared in the parent class will not be called automatically. To call a destructor declared in a parent class, refer to the parent::__destruct() method.

And in php4: The need to call destructors arises only when working with objects that use a large amount of resources, since all variables and objects are automatically destroyed when the script is completed, but you can use unset() - delete the variable.

Inheritance is made using the service word extends

class Gena
{
function f1($a)
{
echo $a;
}
}

class Roma extends Gena
{

}

$obj=new Roma;
$obj->f1('Argument'); Argument

Or constants that are denoted by the service word const variables that are marked with the service word static

class Gena 
{
const CON='Constant value<br>';
static $per='Static variable<br>';
}

echo Gena::CON; The value of the
constant echo Gena::$per; Static variable

Neither const nor static can be accessed via ->

To access properties and methods, the class declaration uses the self and parent keywords. Example of use :: in the class declaration:

class Gena 
{
const CON='Constant value<br>';
}
class Rita extends Gena
{
static $per='Static variable<br>';
function f_1()
{
echo parent::CON;
echo self::$per;
}
}

Rita::f_1(); The value of the static variable constant

All Methods (Functions) in the Default Public and Static Class

All Properties (Variables) in the Default Public Class

Polymorphism (multiformity) is a consequence of the idea of inheritance.

class Gena 
{
function f_1()
{
echo "<h2>Base Class Function</h2>";
}
function f_2()
{
$this->f_1();
}
}
class Vera extends Gena
{
function f_1()
{
echo "<h3>Class derivative function</h3>";
}
}
$g=new Gena();
$v=new Vera();
$g->f_2(); Base class
function$v->f_1(); Derived class
function$v->f_2(); Derived class function

Turning to the same function f_2() - from different objects we get a different result, in fact we use different functions - this is the essence of polymorphism

class K_1 
{
function f_1()
{
echo 123;
}
function f_2()
{
$this->f_1();
}
}

class K_2 extends K_1
{
function f_1()
{
echo 456;
}
}

$o_1=new K_1;
$o_2=new K_2;
$o_1->f_2(); 123
$o_2->f_2(); 456

Copying Objects

class K_1 
{
function f_1()
{
echo 123;
}
}

$o_1=new K_1;
$o_2=$o_1;
$o_2->f_1(); 123

Compare objects

class K_1 
{
function f_1()
{
echo 123;
}
}

$o_1=new K_1;
$o_2=new K_1;
if ($o_1==$o_2) {echo 'Objects are equal';} // Objects are equal

public/private/protected - access modifiers for methods and properties for php5

  • The public modifier allows you to access properties and methods from anywhere.
  • The private modifier allows you to access properties and methods only within the current class.
  • The protected modifier allows you to access the properties and methods of only the current class and the class that inherits the properties and methods of the current class.

Creating a copy of an object with absolutely identical properties is not always an acceptable option. For example, when your object contains a reference to some other object in use, and when you create a copy of the reference object, you must also create a new instance of the contained object, so that the copy of the object contains its own separate instance of the contained object.

class Gena 
{
public $r='Red<br>';
}
$obj=new Gena();
$obj2=$obj;

$obj->r='Green<br>';
echo $obj->r; Green
echo $obj 2->r; Green

A copy of the object is created using a clone call (which calls the object's __clone() method, if possible). You can declare the __clone() method, which will be called when the object is cloned (after all the properties have been copied from the original object).

class Gena 
{
public $r='Red<br>';
}

$obj=new Gena();
$obj2=clone $obj;

$obj->r='Green<br>';
echo $obj->r; Green
echo $obj 2->r; Red

When a programmer requests that a copy of an object be created, PHP 5 will determine whether the __clone() method has been declared for that object or not. If not, the default __clone() method will be called, which will copy all the properties of the object. If the __clone() method has been declared, it is entirely up to it to create copies of the properties in the object copy. For convenience, the engine provides the programmer with a function that imports all the properties from the source object, so that the programmer can perform a meaningful copy of the properties and override only the necessary ones. Here is an example of cloning an object:

class Car
{
public $year='2011';
public $speed;
public $model;

function __construct($m, $s)
{
$this->model=$m;
$this->speed=$s;
}

function __destruct()
{
echo "Object deleted!";
}

function __clone()
{
$this->year='1965';
$this->model='vaz';
$this->speed=90;
}

function getInfo()
{
echo "Model=".$this->model." <br />";
echo "Speed=".$this->speed." <br />";
echo "Year=".$this->year." <br /><br />";
}
}

$car1=new Car('opel',100);
$car1->getInfo();
$car2=new Car('audi',120);
$car2->year=2009;
$car2->getInfo();
$car3=clone $car1;
$car3->getInfo();
/*
Model=opel
speed=100
Year=2011
Model=audi
speed=120
Year=2009
Model=vaz
speed=90
Year=1965

Object deleted!Object deleted!Object deleted! Object deleted!

*/

It's a go:

Model = opel
speed = 100
Year = 2011

Model = audi
speed = 120
Year = 2009

Model = vaz
speed = 160
Year = 1965

Object deleted!Object deleted!Object deleted!

Class Constants

 

You can now include constants in class definitions, and reference them using an object. Constants can also be declared within the same class. The difference between variables and constants is that the $ symbol is not used when declaring or accessing the latter. Like properties and methods, the values of constants declared inside a class cannot be retrieved through a variable that contains an instance of that class.

class Gena 
{
const CON="Constant";
}

echo Gena::CON; Constant

Static Class Members

 

Class definitions can now include static class members (properties and methods) that are accessed through the class. The general use of static members is shown in the following example:

class Singleton 
{
static private $instance=NULL;
private function __construct()
{

}

static public function getInstance()
{
if (self::$instance==NULL)
{
self::$instance = new Singleton();
}
return self::$instance;

}
}

Static Methods

You can now define methods as static, allowing them to be called outside the context of the object. Static methods are not defined through the variable $this because they do not have to be restricted to a specific object.

class Gena 
{
static function f_1()
{
echo "Hello world!";
}
}
Gena::f_1(); Hello world!

Abstract classes

 

PHP 5 supports the definition of abstract classes and methods. You cannot create an instance of a class that has been declared abstract. A class in which at least one abstract method is declared must also be declared abstract. Methods declared as abstract carry essentially only descriptive meaning and cannot include any functionality. A class can be declared as abstract by using the abstract keyword to exclude the engine from processing the description of the class. However, you can inherit abstract classes. Case study:

abstract class Gena 
{
public $r='Red';
}

class Petya extends Gena
{
}

$o=new Petya;
echo $o->r; Red