Jump to content

So I was just reading php.net...


TJMAudio

Recommended Posts

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:

[code]<?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());

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/23496-so-i-was-just-reading-phpnet/
Share on other sites

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.

[url=http://php.net/manual/en/language.oop5.interfaces.php]More info on interfaces[/url]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.