Jump to content


Photo

So I was just reading php.net...


  • Please log in to reply
1 reply to this topic

#1 TJMAudio

TJMAudio
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 10 October 2006 - 04:08 AM

Then I was browsing php.net.  I came across some function, I forget what it was, but then it contained a very complex, almost C# looking, PHP script.  I was just wondering what it did?  Mainly one function it used a lot, and that was "interface".  Here is the script:

<?php

interface INamed
{
   public function getName();
}
interface ISon
{
   public function getFather();
}
interface IFather
{
   public function getSons();
}

abstract class Named implements INamed
{
   protected $name;
   public function __construct($name)
   {
       $this->name = $name;
   }
   public function __destruct()
   {}
   public function getName()
   {
       return $this->name;
   }
}
abstract class Son implements ISon
{
   protected $father;
   public function getFather()
   {
       return $this->father;
   }
   public function __construct(IFather $father = null)
   {
       $this->father = $father;
   }
   public function __destruct()
   {}
}
abstract class Father implements IFather
{
   protected $sons = array();
   public function getSons()
   {
       return $this->sons;
   }
   public function __construct($sons = array())
   {
       $this->sons = $sons;
   }
   public function __destruct()
   {}
}

class Man implements INamed, ISon, IFather
{
   public function __construct($name, IFather $father = null, $sons = array())
   {
       // note: these are just __construct() calls - not real class initializations
       Named::__construct($name);
       Son::__construct($father);
       Father::__construct($sons);
   }
   public function __destruct()
   {
       Named::__destruct();
       Son::__destruct();
       Father::__destruct();
   }
   public function getFather()
   {
       return Son::getFather();
   }
   public function getSons()
   {
       return Father::getSons();
   }
   public function getName()
   {
       return Named::getName();
   }
}

$siarhej = new Man('Siaroh', new Man('Adas'), array(new Man('Seva')));

var_dump($siarhej->getName());
var_dump($siarhej->getFather()->getName());
$siarhejSons = $siarhej->getSons();
var_dump($siarhejSons[0]->getName());

?>

:: My Site ::
Feel free to critique...

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 10 October 2006 - 05:33 AM

Interfaces are sort of like templates for classes (PHP5 or higher only). When a class implements an interface, then it must contain the elements that the interface contains.

More info on interfaces




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users