简体   繁体   中英

Child class properties populated with parent class methods

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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