duan0530 2011-06-22 22:38
浏览 40
已采纳

为我的所有类实现/扩展/继承/ ...相同的__get和__set函数

I do not like functions like "getProperty" and "setProperty", so I looked into the __get and __set functions.

I created a structure that works quite well. Now I want to use it in most of my classes. How do I do this (keeping in mind I might want to extend a class with another class) without having to duplicate code?

I already know an interface is not an option, as that's all about duplicating code (if I understand it correctly)

Here are the functions, just in case:

/**
 * Simply return the property
 */
private function __get($strProperty) {

    if(array_key_exists($strProperty, $this->properties)){

        $c = $this->properties[$strProperty];

        // Fetch the wanted data and store it in memory
        if(!$c['fetched']){

            if($c['type'] == 'array'){
                $proptr = $this->md->fetch_array('SELECT ' . $c['field'] . ' FROM ' . $c['table'] . ' WHERE ' . $c['where'], $c['table'].$c['field'].$c['where'], 1000);
                $c['value'] = $proptr;
            }else {
                $proptr = $this->md->query_first('SELECT ' . $c['field'] . ' FROM ' . $c['table'] . ' WHERE ' . $c['where'], $c['table'].$c['field'].$c['where'], 1000);
                $c['value'] = $proptr[$c['field']];
            }


        }

        return $c['value'];

    } else {
        return $this->$strProperty;
    }

}

/**
 * Set the property, and update the database when needed
 */
private function __set($strProperty, $varValue) {

    // If the property is defined in the $properties array, do something special
    if (array_key_exists($strProperty, $this->properties)) {

        // Get the fieldname
        $field = $this->properties[$strProperty]['field'];

        $data[$field] = $varValue;

        $this->md->update(TABLE_USER, $data, $this->properties[$strProperty]['where']);

        // And store the value here, too
        $this->$strProperty = $varValue;

    } else {
        $this->$strProperty = $varValue;
    }
}
  • 写回答

3条回答 默认 最新

  • dongqie2010 2011-06-22 22:55
    关注

    If I understand you correctly you would like to automate the handing of the property array within a set of objects, if so then this should work accordingly:

    abstract class Prototype
    {
        protected $properties = array();
    
        public function __get($key)
        {
            if(property_exists($this,'properties') && is_array($this->properties))
            {
                return isset($this->properties[$key]) ? $this->properties[$key] : null;
            }
        }
    
        public function __set($key,$value)
        {
            if(property_exists($this,'properties') && is_array($this->properties))
            {
                $this->properties[$key] = $value;
            }
        }
    }
    

    This is just a basic concept but I have just tested and work's fine, you simple extend your root classes like so:

    class User extends Prototype{}
    

    and then use as you would normally set values using get and set magic methods, setting the methods to protected will allow the visibility of the methods to be available in all child classes but will not be allowed outside the object.

    Is this what you was looking for?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图