我是PHP新手,刚刚进入OOP.我有几个用于设置和获取属性的泛型方法.我几乎在所有类中经常使用它们,我将这些方法放在一个类中并从中扩展其他类.现在我可以从子类访问方法,但不知道如何通过它们设置和获取子类的属性... 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;
}
}
Run Code Online (Sandbox Code Playgroud)
子类category.php
class categories extends Base
{
private $id;
private $pid;
private $title;
private $status;
public function __construct()
{
parent::__construct();
}
}
Run Code Online (Sandbox Code Playgroud)
我称之为
$objCat …Run Code Online (Sandbox Code Playgroud)