Answer the question
In order to leave comments, you need to log in
How to force classes to implement the same method from parent class?
Good afternoon!
Let's say there are three classes. One parent and two children.
Example
class Parent
{
public function getName()
{
return "Родитель";
}
}
class Child1 extends Parent
{
public function getName()
{
return "Дочерний 1";
}
}
class Child2 extends Parent
{
public function getName()
{
return "Дочерний 2";
}
}
echo $parent->getName();
echo $child1->getName();
echo $child2->getName();
// Родитель
// Дочерний 1
// Дочерний 2
Answer the question
In order to leave comments, you need to log in
<?php
class p {
public $name = 'p';
function getname(){
return $this->name;
}
}
class c1 extends p {
public $name = 'c1';
}
class c2 extends p {
public $name = 'c2';
}
$p = new p;
$c1 = new c1;
$c2 = new c2;
echo $p->getname();
echo $c1->getname();
echo $c2->getname();
Hello.
Can't you make the parent abstract too?
For example:
abstract class Parent
{
abstract public function getName()
{
return "Родитель";
}
}
class Child1 extends Parent
{
public function getName()
{
return "Дочерний 1";
}
}
class Child2 extends Parent
{
public function getName()
{
return "Дочерний 2";
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question