Last Updated:

OOP in PHP - Basic construct and modifiers

Basic construct and modifiers in PHP OOP

 

OOP has modifiers for accessing properties (variables) and methods of functions:

public or var (properties, methods) - full control

private (properties, methods) - only from its own class

protected (properties, methods) - only from its own class and the descendant class

internal - full access, only from one assembly (without include, etc.) - rarely used

All default properties (variables) public (var)

All default methods (functions) public and static

You need to access properties (variables) and method (functions) within a class through $this->a;

Ave:

class Gena
{
//var $a='Gena'; // the same
public $a='Gena';

function f1()
{
return 'Hi, '.$this->a;
}
}

$ob=new Gena;
//echo $ob->a; Gene
echo $ob->f1(); Hi Gena

Functions (they are static by default) and properties (it is necessary to specify static) - can be accessed without creating an object via ::

Ave:

class Gena
{
public static $i=2;
const CON=8;
function f1($a)
{
echo $a;
}
}
Gena::f1('Gennady'); Gennady
echo Gena::$i; 2
echo Gena::CON; 8

Extends are used to EXPAND the cash register.

Pr: class Gena2 extends Gena {} - new. The (child) Gena2 class is extended by the Gena class. The Gena2 class retrieves all the properties and methods from the gena class and they can be reassigned in the child class (gena2)

To declare a CONSTANT, we use the service word const. Neither const nor static can be accessed through ->

 

To refer to the properties and methods of the class, use the keywords self (within the class) and parent (to the parent classes). An example of using extends and :: in a class declaration:

Ave:

class MyClass 
{
function __call($name, $params)
{
echo "A $name method was called with the parameter $params[0]";
}
}
$obj = new MyClass;
echo $obj->method(1); the method method is called with parameter 1

__construct($a) and __destruct() special functions

function __construct($a) - CONSTRUCTOR function, automatically runs when creating an object $ob=new Gena($a), you can pass arguments. function __construct($a) can be replaced with the function gena($a) class name.

Parent __construct can only be queried in child, only through parent::__construct($a);

Ave:

class Gena 
{
const CON='Constant ';
}

class Gena2 extends Gena
{
function __construct($a)
{
echo $a;
}
}
$ob=new Gena2('Gena'); Gene

function __destruct() - the DESTRUCTOR function deletes the entire object (frees up memory), but after working out the code, all objects are automatically destroyed. By analogy, you can use parent::__destruct(),but you can also use unset() - deleting a variable.

Cloning objects

If you write $ob 2=$ob; (this is link) - $ob 2 means a reference to the $ob object. If you change one object, another object changes. For cloning (creating another similar object), you need to use the clone keyword

Ave:

class Gena
{
public $a=1;
}
$ob=new Gena;
$ob2=clone $ob;
$ob2->a=2;

echo $ob->a; 1
echo $ob2->a; 2

You can create a clone method, function __clone(), which will automatically fire when cloned.

Ave:

class Gena
{
public $a=1;
function __clone()
{
$this->a=2;
}
}
$ob=new Gena;
$ob2=clone $ob;

echo $ob->a; 1
echo $ob2->a; 2

A method with the final keyword cannot be overridden in child classes. A class with the final keyword cannot have heirs.

Abstract classes (abstract class Gena) - classes that cannot have objects. Objects can only have their heirs.

An abstract method ( abstract public function f1(){} ) can only reside in an abstract class.

The main difference between interfaces and abstract classes is that in PHP 5 a class cannot be derived from several classes, including abstract ones, but can be created on the basis of any number of interfaces.

In this case, in the interface, methods are declared by the function keyword without specifying any specifiers, including abstract.

Ave:

interface Int1 
{
function func1();
}

interface Int2
{
function func2();
}

class MyClass implements Int1, Int2
{
public function func1()
{
echo 'Vasya -';
}

public function func2()
{
echo ' friend!';
}
}

$obj=new MyClass;
$obj->func1(); Vasya -
$obj->func2(); A friend!

Thus, although multiple inheritance is not supported in PHP 5, developers have a real opportunity to create classes based on separately described interfaces.

The special instanceof keyword in PHP 5 allows you to determine whether an object is an instance of a particular class, or an instance of a class derived from some other class.

Ave:

class MyClass {}
$obj1=new MyClass();

if ($obj 1 instanceof MyClass)
{
echo "\$obj 1 - object of class MyClass<br>\n";
}

class MyClass1 extends MyClass {}
$obj2=new MyClass1();

if ($obj 2 instanceof MyClass)
{
echo "\$obj 2 - class object derived from MyClass<br>\n";
}

interface Int {}
class MyClass2 implements Int {}
$obj3 = new MyClass2();

if ($obj 3 instanceof Int)
{
echo "\$obj 3 - class object created on the basis of the Int<br>\n interface";
}
/*
$obj 1 - object of class MyClass
$obj 2 - object of class derived from MyClass
$obj 3 - object of a class created on the basis of the Int interface

*/

You can also use instanceof to determine whether an object is an instance of a class created from a specific interface.

The special function __autoload() will be called in PHP 5 if you try to create an object of an undefined class.

Ave:

function __autoload($class) 
{
echo "an attempt to create an object of an undefined class", $class;
}
$obj=new MyClass;

As a parameter to the __autoload() function, the name of an indeterminate class is passed.

In PHP 5, you can overload access to object properties.

Ave:

class MyClass 
{
private $properties;
function __set($name, $value)
{
echo "setting a new $name property = $value<br>\n";
$this->properties[$name]=$value;
}

function __get($name)
{
echo "read the value of the $name<br>\n property";
return $this->properties[$name];
}
}

$obj=new MyClass;
$obj->property=1; set a new property = 1
$a=$obj->property; Read the value of the $a property
echo property. 1

The new __get() and __set() accessors make it easy to dynamically assign properties to objects. Property names are passed to these methods as parameters.

The __set() method also gets the value that is set for the property.

When you call a non-existent object method in PHP 5, the special __call() method is automatically called.

Ave:

class MyClass 
{
function __call($name, $params)
{
echo "The $name method was called with the parameter $params[0]";
}
}
$obj = new MyClass;
echo $obj->method(1); the method method is called with parameter 1

As parameters, the __call() takes the name of the method being called and the parameters passed to this method.

In PHP 5, you can completely enumerate all the properties of an object in the foreach statement.

Ave:

class MyClass 
{
public $a = 1;
public $b = 2;
}
$obj=new MyClass;

foreach ($obj as $name => $value)
{
echo "$name => $value "; a => 1 b => 2

}

PHP 5's special IteratorAggregate and Iterator interfaces allow you to specify the behavior of a class when it is used with the foreach statement.

In PHP 5, the pseudo-constant __METHOD__ returns the class name and the method to be called.

Ave:

class MyClass 
{
public function myMethod()
{
echo "call the method ".__METHOD__." <br>\n";
}
}
$obj=new MyClass;
$obj->myMethod(); call the MyClass::myMethod

function myFunction()
{
echo "call the function ".__METHOD__." <br>\n";
}
myFunction(); call the myFunction function

When a function is accessed outside the __METHOD__ class, it returns only the name of the function.

PHP 5 introduces another special class method, __toString().

Ave:

class MyClass 
{
function __toString()
{
return "the __toString()" method called;
}
}
$obj=new MyClass;
echo $obj; the __toString() method is called

The __toString() method of class overloads the conversion of an object to a string.

PHP 5 introduces the ability to dereferencing objects that are returned by functions.

Ave:

class MyClass1 
{
public function showClassName()
{
echo "object of class MyClass1<br>\n";
}
}

class MyClass2
{
public function showClassName()
{
echo "object of class MyClass2<br>\n";
}
}

function deref($obj)
{
switch ($obj)
{
case "MyClass1":return new MyClass1();
case "MyClass2":return new MyClass2();
}
}
deref("MyClass1")->showClassName(); Object of class MyClass1
deref("MyClass2")->showClassName(); object of class MyClass2

This mechanism allows you to call methods on objects whose class names are returned by user-defined functions.

PHP 5 implements special built-in classes (Reflection APIs) that allow you to analyze classes and objects to determine the methods, properties, and interfaces they support.

Ave:

class MyClass 
{
public $property;
function myFunc($name)
{
echo $name;
}
}
ReflectionClass::export("MyClass");
ReflectionObject::export(new MyClass);
ReflectionMethod::export("MyClass", "myFunc");
ReflectionProperty::export("MyClass", "property");
ReflectionExtension::export("standard");

Using the ReflectionExtension class, you can similarly parse PHP 5 extensions.

Fixing the OOP construct in PHP

class Person 
{
private $name;

public function __construct($name)
{
$this->name = $name;
}

public function sneeze()
{
echo $this->name, " sneezed<br>\n";
}

public function say($that)
{
echo $this->name, ': - ', $what, "<br>\n";
}
}

$ivanov = new Man("Ivanov");
$sidorov = new Man("Sidorov");

$ivanov->shoot();
$sidorov->said('Be well!');
$ivanov->said('Thank you...');

A few more examples:

<h1>Heritage</h1>
<?php
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
?>


<h1>Encapsulation</h1>
<?php
class Gena4
{
private $a='Gena';
function f1()
{
return $this->a;
}
}
$obj=new Gena4;
//echo $obj->a; MUST NOT!!!
echo $obj->f1(); Gene
?>


<h1>Polymorphism</h1>
<?php
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!!!
?>


<h1>Abstrate class</h1>
<?php
abstract class Dad
{
public function SayHi() {
return "Hello!";
}
abstract public function SayHello();
}
class Son extends Dad
{
public function SayHello() {
return "Great!";
}
}
$son = new Son;
echo $son->SayHello().' - '.$son->SayHi();
?>


<h1>Interfaces</h1>
<?php
interface Planes{
public function TakeOff(); // takeoff
public function Landing(); // landing
}

interface Planes2{
public function Polet(); // flight
}
class Boing implements Planes, Planes2{
public function TakeOff()
{
return "Takeoff"; Implement the TakeOff() method of the Planes interface.
}
public function Landing()
{
return "Landing"; // Implement the Landing() method of the Planes interface.
}
public function Polet()
{
return "Flight"; // Implement the Polet() method of the Planes2 interface.
}
}

$obj=new Boing;
//echo $obj->Landing(); Landing
echo $obj->Polet(); Landing
//echo Boing::TakeOff(); takeoff
?>


<h1>Use of __set() and __get()</h1>

Chessboard

<! DOCTYPE HTML><html dir="ltr" lang="ru-ru">
<head>
<meta charset="utf-8">
<title>Dep</title>
<style>
.cen {text-align:center;}
table {border-collapse:collapse;}
table.doska {margin:10px auto;}
table.doska td {border:1Px solid #000; width:25px; height:25px; text-align:center; font-size:9px;}
table.doska td.bel {background:#fff; color:#000;}
table.doska td.che {background:#000; color:#fff;}
</style>
</head></body>
<h1 class="cen">Shmallow</h1>
<?php
class Doski
{

private function doska($n)
{
$r="\n\n<table class='doska'>\n";
for($iv=1; $iv<=$n; $iv++)
{
$r.="<tr>";
if ($iv%2==0){$kl='bel';} else{$kl='che';}
for ($ig=1; $ig<=$n; $ig++)
{
if ($kl=='che'){$kl='bel';} else{$kl='che';}
$r.="<td class='$kl'>";
$r.="$iv$ig";
$r.="</td>";
}
$r.="<tr>\n";
}
return $r.="</table>\n\n";
}

public function krat($n=8,$i=1)
{
for($j=1,$j<=$i,$j++)
echo $this->doska($n++);
}
}

$ob=new doski();
$ob->krat(1,8);
?>

</head><body>