MENU
Undefined Methods and Traits
An abstract method is an undefined method. A class containing an abstract method must be declared abstract, and may not be instantiated. If a child class extending an abstract class does not define all its abstract methods, the child class must be declared abstract too.RESETRUNFULL
<!DOCTYPE html>
<html>
<head></head>
<body><?php
abstract class P{
protected $v1=3;
protected $v2=2;
abstract function calculate();
}
class product extends P{
function calculate(){
echo $this->v1*$this->v2;
}
}
$a=new product;
$a->calculate();
?></body>
</html>
RESETRUNFULL
<!DOCTYPE html>
<html>
<head></head>
<body><?php
interface I1{
const c=100;
}
interface I2{
const d=200;
}
interface STAR extends I1,I2{
public function star();
}
interface CROSS{
public function cross();
}
class C implements STAR,CROSS{
public function star(){
echo C::c * C::d."<br />";
}
public function cross(){
echo C::c + C::d."<br />";
}
}
$a=new C;
$a->star(); //20000
$a->cross(); //300
echo I1::c; //100
?></body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body><?php
trait T{
public $v=3;
private function shout(){
echo "Hello World!<br />";
}
}
class P{
function shout(){
echo "Hello Universe!<br />";
}
}
class C extends P{
use T; // overrides shout() from P
function shout(){ // overrides shout() from T
echo "Smile World!<br />";
}
}
$o=new C;
$o->shout();
echo $o->v;
?></body>
</html>
Smile World! 3
<!DOCTYPE html>
<html>
<head></head>
<body><?php
trait T1{
function shout(){
echo "Hello World!<br />";
}
}
trait T2{
use T1 {shout as protected;}
function smile(){
echo "Smile World!<br />";
}
}
trait T3{
function shout(){
echo "Hello Universe!<br />";
}
function smile(){
echo "Smile Universe!<br />";
}
}
class C{
use T2,T3{
T2::shout insteadof T3;
T3::smile insteadof T2;
T2::smile as private sm;
}
function start(){
$this->shout();
$this->smile();
$this->sm();
}
}
$o=new C;
$o->start();
?></body>
</html>
Hello World! Smile Universe! Smile World!