<?php
define("BASDIR",__DIR__);
include BASDIR."/Phpclass/Loader.php";
spl_autoload_register("\\Phpclass\\Loader::autoload_rege");
class page
{
protected $ihobby;//0、这里主要是创建一个空对象,通过第二步转移过来
function index()
{
$this->ihobby->showadds();//3、最后通过对象本身来调用,自己的方法
$this->ihobby->showmore();
}
function set_hobby(\Phpclass\Ihobby $Ihobby)//这里指明了空间类,当然不指名,试了一下,也没有出现问题
//可能是限定在这个空间或这个类中.
{
$this->ihobby = $Ihobby;//转移对对象
}
}
$page =new page();
if(isset($_GET['male']))
{
$Ihobby = new \Phpclass\Malehobby();//1、这里根据不同的值来创建对象(实例化)
}else
{
$Ihobby = new \Phpclass\Femalhobby();//1、这里根据不同的值来创建对象
}
$page->set_hobby($Ihobby);//2、这里把创建的对象,转移给之前创建的空对象
$page->index();
----------------------------------------------------------------------------index3.php
<?php
namespace Phpclass;
interface Ihobby
{
function showadds();
function showmore();
}
----------------------------------------------------------------------------Ihobby.php
<?php
namespace Phpclass;
class Malehobby implements Ihobby
{
function showadds()
{
echo "this is male hobby\n";
}
function showmore()
{
echo "this is the contents for male";
}
}
----------------------------------------------------------------------------Malehobby.php
<?php
namespace Phpclass;
class Femalhobby implements Ihobby
{
function showadds()
{
echo "this is femal hobby-add\n";
}
function showmore()
{
echo "this is the contents for Femal";
}
function atest()//可以除了接口规定的函数外,还可以有其它函数
{
echo "test";
}
}
----------------------------------------------------------------------------Femalehobby.php