简体   繁体   English

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

[英]Child class properties populated with parent class methods

I am new to PHP and just get into OOP. 我是PHP新手,刚刚进入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 现在我可以从子类访问方法,但不知道如何通过它们设置和获取子类的属性... 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 子类category.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 如果是这样的话怎么做...谢谢

Extending a class is something you only want to do when you can say class categories is a class Base . 当你可以说类categories是类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. PHP也有推出一些所谓的特质复制/粘贴代码。 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 . 有关SOLID编程中 Liskov替换原则的更多信息,请参见参考资料。

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. 如果由我决定,我会以任何方式避免那些神奇的getter / setter,只需在课堂上添加你自己的getter / setters方法。

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 . 想想一些类Database和类Article Because the Article class needs access to the database many people let the class extend the Database class. 因为Article类需要访问数据库,所以许多人让类扩展Database类。 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. 因此,在扩展课程时,您必须能够说B类是A类。

Some other notes about your code: 关于您的代码的一些其他说明:

  • Always make your class names PascalCase. 总是让你的类名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. 我的个人偏好有点:请总是在你的foreach语句中添加大括号。 It is more clear what is happening when other people are reading your code. 当其他人阅读您的代码时,更清楚的是发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM