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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应