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>
An interface contains only constants and undefined public methods. It may not be instantiated on its own. Any class that implements an interface must define all its methods. A class cannot implement two interfaces that share function or constant names, and an interface constant cannot be overridden.

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>
A trait cannot be instantiated on its own. A class can use multiple traits at once. A method defined inside a class overrides the trait method, which in turn overrides the method from the parent class.

<!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
A trait can use another trait. If two traits used contain the same function names, they need to be resolved with insteadof. An alias name can be assigned to a function with as, which may also change the visibility.

<!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!
A trait method can be static. It can also be abstract, which forces the class that uses it to define it. Traits can’t contain constants.