龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php class定义

时间:2015-01-17 11:12来源:网络整理 作者:网络 点击:
分享到:
php class定义 [代码片段(42行)]
<?php
class PersonWriter {

    function writeName( Person $p ) {
        print $p->getName().'\n';
    }

    function writeAge( Person $p ) {
        print $p->getAge().'\n';
    }
}

class Person {
    private $writer;

    function __construct( PersonWriter $writer ) {
        $this->writer = $writer;
    }

    function __call( $method, $args ) {
        if ( method_exists( $this->writer, $method ) ) {
            return $this->writer->$method( $this );
        }
    }

    function getName()  { 
        return 'Joe'; 
    }
    function getAge() { 
        return 44; 
    }
}

$person= new Person( new PersonWriter() );
$person->writeName();
$person->writeAge();
?>

收藏文章
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
评论内容为空!
还没有评论,快来抢沙发吧!
按钮 内容不能为空!
立刻说两句吧! 查看0条评论
精彩图集

赞助商链接