+ -
当前位置:首页 → 问答吧 → 奉献一个oo的基砖,能用请用之

奉献一个oo的基砖,能用请用之

时间:2009-11-13

来源:互联网

本帖最后由 huozhe3136 于 2009-11-14 07:32 编辑

很久都没有上phpchina了,今天来,突然发现自己竟成贵宾,哇,太感动了
一个本人写的php oo基类,作为对开源社区的一点贡献吧,能用请用之,欢迎拍砖!
感兴趣的牛人一起加入,一起来完善php,成果是大家的,也希望大家来共同参与
project 地址: http://code.google.com/p/php-basic-objects/
加入的话,加我msn: [email protected],我会告诉你加入的密码

如果你只是想用用代码:
svn checkout http://php-basic-objects.googlecode.com/svn/trunk/ php-basic-objects[code]
下面是原始版本:
<?php
/**
* 可以为所有类的基类
* 感谢开源社区和所有为开源贡献的coder
* 尊重他人的劳动请保留原作者信息
* 版权遵从GPL
* 如果你有什么改进的建议请联系我
* @author [email protected]
* @version 0.01 lastmodify 2009-11-14 06:08
*/
class Object{
   
    public function __construct(){
        
    }
   
    /**
     * 打印该类的方法和属性,用于显示帮助
     * @return void
     */
    public function __help(){        
        $class = $this->__toString();
        $vars = $this->__getPropertys();         
        $methods = $this->__getMethods();
        echo "Class:$class\n";
        echo "\tPropertys:\n";
        foreach($vars as $varname=>$value)  echo "\t\t\$varname: $value\n";
        echo "\tFunctions:\n";
        foreach($methods as $m)
            if(!in_array($m,array('__toString','__get','__set'))) echo "\t\t$m()\n";
    }

    /**
     * 返回类的方法
     * @return array
     */
    public function __getMethods(){
        return get_class_methods($this);
    }
   
    /**
     * 返回类的属性
     * @return array
     */
    public function __getPropertys(){
        return get_class_vars(get_class($this));
    }
   
    /**
     * 默认地php中的类的实例不能访问类静态成员,需要通过self来调用
     * 通过重写__get魔术函数,来实现用$this调用类的静态成员
     */
    public function __get($var){
        $methods = $this->__getMethods();
        $getter = 'get'.ucfirst($var);
        if(in_array($getter,$methods)) return $this->$getter();
        if(isset($this->$var))
            return $this->$var;
        elseif(isset(self::${$var}))
           return self::${$var};
        return;
    }
   
    /**
     * 默认地php中的类的实例不能访问类静态成员,需要通过self来调用
     * 通过重写__set魔术函数,来实现用$this调用类的静态成员
     * 子类可以通过声明setter 但不赋值来实现只读属性
     */
    public function __set($var,$value){
        $methods = $this->__getMethods();
        $setter = 'set'.ucfirst($var);
        if(in_array($setter,$methods)) return $this->$setter($value);
        if(isset($this->$var)){
            $this->$var = $value;
            return $this;
        }elseif(isset(self::${$var})){
            self::${$var} = $value;
            return $this;
        }
        return;
    }
   
    /**
     * php魔术函数,用来返回类名
     * 调用方式:
     * <code>
     * $var = new Object();
     * echo $var;
     * </code>
     * @return string
     */
    public function __toString(){
        return get_class($this);
    }
   
    public function __call($method,$argv){
        return;
    }
   
}

[/code]接下来,新建一个文件Person.php用来测试,
这里我们假定Person有三个属性 name,age,blood.
blood表示一个血型,对象创建后是不能改变的,也就是只读属性,以前老是看论坛里说php没办法实现只读,now,you see :)[code]<?php
require_once "Object.php";
class Person extends Object{
    private $name;
    private $age;
    private $blood;

    public function __construct($blood){
        $this->blood = $blood;
    }

    public function getName(){
        return $this->name;
    }

    public function setName($value){
        $this->name = $value;
        return $this;
    }

    public function getAge(){
        return $this->age;
    }

    public function setAge($value){
        if($value<0 || $value>100){
            echo "error: age must be >0 and <100\n";
        }else{
            $this->age = $value;
        }
    }

    public function getBlood(){
        return $this->blood;
    }

    public function setBlood(){
        echo "You can't chage my blood,I'm readOnly property\n";
        return false;
    }

}
[/code]下面为测试代码[code]<?php
require_once "Person.php";
$a = new Person('a');
$a->name = "huozhe3136";
echo "My name is ". $a->name ."\n";
$a->age = 130;
$a->age = 30;
echo "My age is large than ".$a->age."\n";
echo "My blood is ".$a->blood."\n";
$a->blood = "o";

echo "You can get help by call Object::__help();\n";
$a->__help();[/code]测试时输出:[code][root /home/php]> php test.php
My name is huozhe3136
error: age must be >0 and <100
My age is large than 30
My blood is a
You can't chage my blood,I'm readOnly property
You can get help by call Object::__help();
Class:Person
        Propertys:
        Functions:
                __construct()
                getName()
                setName()
                getAge()
                setAge()
                getBlood()
                setBlood()
                __help()
                __getMethods()
                __getPropertys()
                __call()[/code] phpObject_20091114_0630.rar (1.67 KB)
下载次数: 6
2009-11-14 06:26

作者: huozhe3136   发布时间: 2009-11-13

学习了.....

作者: whl1295a   发布时间: 2009-11-14

学习啦。。。。

作者: zmjsg   发布时间: 2009-12-11

热门下载

更多