Php class extends functions

Php class extends functions

This example will hopefully help you see how abstract works, how interfaces work, and how they can work together. This example will also work/compile on PHP7, the others were typed live in the form and may work but the last one was made/tested for real:

// Define things a product *has* to be able to do (has to implement)
interface productInterface public function doSell ();
public function doBuy ();
>

// Define our default abstraction
abstract class defaultProductAbstraction implements productInterface private $_bought = false ;
private $_sold = false ;
abstract public function doMore ();
public function doSell () /* the default implementation */
$this -> _sold = true ;
echo «defaultProductAbstraction doSell: < $this ->_sold > » . ¶ ;
>
public function doBuy () $this -> _bought = true ;
echo «defaultProductAbstraction doBuy: < $this ->_bought > » . ¶ ;
>
>

class defaultProductImplementation extends defaultProductAbstraction public function doMore () echo «defaultProductImplementation doMore()» . ¶ ;
>
>

class myProductImplementation extends defaultProductAbstraction public function doMore () echo «myProductImplementation doMore() does more!» . ¶ ;
>
public function doBuy () echo «myProductImplementation’s doBuy() and also my parent’s dubai()» . ¶ ;
parent :: doBuy ();
>
>

class myProduct extends defaultProductImplementation private $_bought = true ;
public function __construct () var_dump ( $this -> _bought );
>
public function doBuy () /* non-default doBuy implementation */
$this -> _bought = true ;
echo «myProduct overrides the defaultProductImplementation’s doBuy() here < $this ->_bought > » . ¶ ;
>
>

class myOtherProduct extends myProductImplementation public function doBuy () echo «myOtherProduct overrides myProductImplementations doBuy() here but still calls parent too» . ¶ ;
parent :: doBuy ();
>
>

echo «new myProduct()» . ¶ ;
$product = new myProduct ();

$product -> doBuy ();
$product -> doSell ();
$product -> doMore ();

echo ¶ . «new defaultProductImplementation()» . ¶ ;

$newProduct = new defaultProductImplementation ();
$newProduct -> doBuy ();
$newProduct -> doSell ();
$newProduct -> doMore ();

echo ¶ . «new myProductImplementation» . ¶ ;
$lastProduct = new myProductImplementation ();
$lastProduct -> doBuy ();
$lastProduct -> doSell ();
$lastProduct -> doMore ();

echo ¶ . «new myOtherProduct» . ¶ ;
$anotherNewProduct = new myOtherProduct ();
$anotherNewProduct -> doBuy ();
$anotherNewProduct -> doSell ();
$anotherNewProduct -> doMore ();
?>

Will result in:
/*
new myProduct()
bool(true)
myProduct overrides the defaultProductImplementation’s doBuy() here 1
defaultProductAbstraction doSell: 1
defaultProductImplementation doMore()

new defaultProductImplementation()
defaultProductAbstraction doBuy: 1
defaultProductAbstraction doSell: 1
defaultProductImplementation doMore()

new myProductImplementation
myProductImplementation’s doBuy() and also my parent’s dubai()
defaultProductAbstraction doBuy: 1
defaultProductAbstraction doSell: 1
myProductImplementation doMore() does more!

new myOtherProduct
myOtherProduct overrides myProductImplementations doBuy() here but still calls parent too
myProductImplementation’s doBuy() and also my parent’s dubai()
defaultProductAbstraction doBuy: 1
defaultProductAbstraction doSell: 1
myProductImplementation doMore() does more!

Also you may set return/arguments type declaring for abstract methods (PHP>=7.0)
declare( strict_types = 1 );

abstract class Adapter
protected $name ;
abstract public function getName (): string ;
abstract public function setName ( string $value );
>

Читайте также:  Javascript if string contains character

class AdapterFoo extends Adapter
public function getName (): string
return $this -> name ;
>
// return type declaring not defined in abstract class, set here
public function setName ( string $value ): self
$this -> name = $value ;
return $this ;
>
>
?>

The documentation says: «It is not allowed to create an instance of a class that has been defined as abstract.». It only means you cannot initialize an object from an abstract class. Invoking static method of abstract class is still feasible. For example:
abstract class Foo
static function bar ()
echo «test\n» ;
>
>

Here is another thing about abstract class and interface.

Sometimes, we define an interface for a `Factory` and ease out some common methods of the `Factory` through an `abstract` class.

In this case, the abstract class implements the interface, but does not need to implement all methods of the interface.

The simple reason is, any class implementing an interface, needs to either implement all methods, or declare itself abstract.

Because of this, the following code is perfectly ok.

interface Element /**
* Constructor function. Must pass existing config, or leave as
* is for new element, where the default will be used instead.
*
* @param array $config Element configuration.
*/
public function __construct ( $config = [] );

/**
* Get the definition of the Element.
*
* @return array An array with ‘title’, ‘description’ and ‘type’
*/
public static function get_definition ();

/**
* Get Element config variable.
*
* @return array Associative array of Element Config.
*/
public function get_config ();

/**
* Set Element config variable.
*
* @param array $config New configuration variable.
*
* @return void
*/
public function set_config ( $config );
>

abstract class Base implements Element

/**
* Element configuration variable
*
* @var array
*/
protected $config = [];

/**
* Get Element config variable.
*
* @return array Associative array of Element Config.
*/
public function get_config () return $this -> config ;
>

/**
* Create an eForm Element instance
*
* @param array $config Element config.
*/
public function __construct ( $config = [] ) $this -> set_config ( $config );
>
>

class MyElement extends Base

public static function get_definition () return [
‘type’ => ‘MyElement’ ,
];
>

public function set_config ( $config ) // Do something here
$this -> config = $config ;
>
>

$element = new MyElement ( [
‘foo’ => ‘bar’ ,
] );

print_r ( $element -> get_config () );
?>

You can see the tests being executed here and PHP 5.4 upward, the output is consistent. https://3v4l.org/8NqqW

Ok. the docs are a bit vague when it comes to an abstract class extending another abstract class. An abstract class that extends another abstract class doesn’t need to define the abstract methods from the parent class. In other words, this causes an error:

Читайте также:  Python узнать тип элемента

abstract class class1 <
abstract public function someFunc ();
>
abstract class class2 extends class1 <
abstract public function someFunc ();
>
?>

Error: Fatal error: Can’t inherit abstract function class1::someFunc() (previously declared abstract in class2) in /home/sneakyimp/public/chump.php on line 7

abstract class class1 <
abstract public function someFunc ();
>
abstract class class2 extends class1 <
>
?>

An abstract class that extends an abstract class can pass the buck to its child classes when it comes to implementing the abstract methods of its parent abstract class.

An interface specifies what methods a class must implement, so that anything using that class that expects it to adhere to that interface will work.

eg: I expect any $database to have ->doQuery(), so any class I assign to the database interface should implement the databaseInterface interface which forces implementation of a doQuery method.

interface dbInterface public function doQuery ();
>

class myDB implements dbInterface public function doQuery () /* implementation details here */
>
>

$myDBObj = new myDB ()-> doQuery ();
?>

An abstract class is similar except that some methods can be predefined. Ones listed as abstract will have to be defined as if the abstract class were an interface.

eg. I expect my $person to be able to ->doWalk(), most people walk fine with two feet, but some people have to hop along 🙁

interface PersonInterface () /* every person should walk, or attempt to */
public function doWalk ( $place );
/* every person should be able to age */
public function doAge ();
>

abstract class AveragePerson implements PersonInterface () private $_age = 0 ;
public function doAge () $this -> _age = $this -> _age + 1 ;
>
public function doWalk ( $place ) echo «I am going to walk to $place » . PHP_EOL ;
>
/* every person talks differently! */
abstract function talk ( $say );
>

class Joe extends AveragePerson public function talk ( $say ) echo «In an Austrailian accent, Joe says: $say » . PHP_EOL ;
>
>

class Bob extends AveragePerson public function talk ( $say ) echo «In a Canadian accent, Bob says: $say » . PHP_EOL ;
>
public function doWalk ( $place ) echo «Bob only has one leg and has to hop to $place » . PHP_EOL ;
>
>

$people [] = new Bob ();
$people [] = new Joe ();

foreach ( $people as $person ) $person -> doWalk ( ‘over there’ );
$person -> talk ( ‘PHP rules’ );
>
?>

Источник

Наследование классов

Представьте, что у вас есть класс User . Он нужен вам для каких-то целей и в общем-то полностью вас устраивает — доработки этому классу в не нужны.

А теперь представим себе ситуацию, когда нам понадобился еще и класс Employee . Работник очень похож на юзера, имеет те же свойства и методы, но еще и добавляет свои — свойство salary , а также геттер и сеттер для этого свойства. Вот этот класс:

Читайте также:  Python edit excel file

salary; > // Сеттер зарплаты public function setSalary($salary) < $this->salary = $salary; > public function getName() < return $this->age; > public function setName($name) < $this->name = $name; > public function getAge() < return $this->age; > public function setAge($age) < $this->age = $age; > > ?>

Как мы видим, код классов User и Employee практически полностью совпадает. Было бы намного лучше сделать так, чтобы общая часть была записана только в одном месте.

Для решения проблемы существует такая вещь, как . С помощью наследования мы можем заставить наш класс Employee позаимствовать () методы и свойства класса User и просто дополнить их своими методами и свойствами.

Класс, от которого наследуют называется ( англ. parent ), а класс, который наследует — . Класс-потомок наследует только публичные методы и свойства, но не приватные.

Наследование реализуется с помощью ключевого слова extends (переводится как расширяет ). Перепишем наш класс Employee так, чтобы он наследовал от User :

Проверим работу нового класса Employee :

setSalary(1000); // метод класса Employee $employee->setName(‘john’); // метод унаследован от родителя $employee->setAge(25); // метод унаследован от родителя echo $employee->getSalary(); // метод класса Employee echo $employee->getName(); // метод унаследован от родителя echo $employee->getAge(); // метод унаследован от родителя ?>

Класс-потомок не унаследовал от своего родителя приватные свойства name и age — попытка обратится к ним вызовет ошибку. При этом, однако, в классе-потомке доступны геттеры и сеттеры этих свойств, так как эти геттеры и сеттеры являются публичными.

Не подсматривая в мой код реализуйте такие же классы User , Employee .

Несколько потомков

Преимущества наследования в том, что каждый класс может несколько потомков. Давайте посмотрим на примере. Пусть кроме работника нам нужен еще и класс Student — давайте также унаследуем его от User :

Проверим работу нашего класса:

setCourse(3); // метод класса Student $student->setName(‘john’); // метод унаследован от родителя $student->setAge(25); // метод унаследован от родителя echo $student->getCourse(); // метод класса Student echo $student->getName(); // метод унаследован от родителя echo $student->getAge(); // метод унаследован от родителя ?>

Не подсматривая в мой код реализуйте такой же класс Student , наследующий от класса User .

Наследование от наследников

Пусть у нас есть класс-родитель и класс-потомок. От этого потомка также могут наследовать другие классы, от его потомков другие и так далее. Для примера пусть от класса User наследует Student , а от него в свою очередь наследует класс StudentBSU :

Сделайте класс Programmer , который будет наследовать от класса Employee . Пусть новый класс имеет свойство langs , в котором будет хранится массив языков, которыми владеет программист. Сделайте также геттер и сеттер для этого свойства.

Сделайте класс Driver ( водитель ), который будет наследовать от класса Employee . Пусть новый класс добавляет следующие свойства: водительский стаж, категория вождения (A, B, C, D), а также геттеры и сеттеры к ним.

Источник

Оцените статью