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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.