dourenzhou8740 2012-08-15 07:42
浏览 55
已采纳

使用父类方法填充的子类属性

I am new to PHP and just get into OOP. I have few generic methods used to set and get properties. I use them quite often in almost all the classes, i put those methods in a class and extends other classes from it. Now i can access the methods from child class but dont know how set and get attributes of child class through them ... parent class base.php

class Base
{
    public function __construct()
    {
    }

    function __set($propName, $propValue)
    {
        $this->$propName = $propValue;
    }

    function setProperties(array $data)
    {
        foreach($data as $propName => $propValue)
            $this->$propName = $propValue;
    }

    function __get($propName)
    {
        return (isset($this->$propName))? $this->$propName : "Invalid property!";
    }

    function getProperties(array $properties)
    {
        foreach($properties as $propName)
            $propVals[$propName] = $this->$propName;
        return $propVals;
    }
}

child class categories.php

class categories extends Base
{

    private $id;
    private $pid;
    private $title;
    private $status;

    public function __construct()
    {
        parent::__construct();  
    }
}

and i called it like

$objCat = new categories();

$objCat->setProperties(array('id'=>'10', 'pid'=>'6'));

print_r( $objCat->getProperties(array('id', 'pid')));

Need little guidance here. If its the right way? or at least is it possible to do it like this? if so how to accomplish this ... thanks

  • 写回答

1条回答 默认 最新

  • dream5694 2012-08-15 07:53
    关注

    Extending a class is something you only want to do when you can say class categories is a class Base. Something like that sort of utility class you have their is almost always the wrong way to go. PHP also has introduced something called traits for copy/paste code. However my personal preference is that it is something you will never want to use, because it tightly couples the traits to your class, which is something you want to avoid.

    See for more information the Liskov Substitution principle in SOLID programming.

    If it was up to me I would avoid those magic getters / setters either way and just add your own getters / setters methods to the class.

    The mistake about some base class isn't something only you are doing (hell even I have done it in the past). Think about some class Database and a class Article. Because the Article class needs access to the database many people let the class extend the Database class. This isn't correct because an article isn't an database. Instead an instance of the database class should be injected into the article class by using dependency injection. The instance should either be injected into the class constructor (if all or many methods need access to it) or just the methods that need it. So when extending a class you have to be able to say class B is a class A.

    Some other notes about your code:

    • Always make your class names PascalCase. This is not really required to make your code work, but it follows a naming convention often used.
    • And my personal preference a bit: please always add curly braces to your foreach statements. It is more clear what is happening when other people are reading your code.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测