将 PHP 类构造为父子类的正确方法是什么?
我有以下 PHP 类,
class.property.php
class.location.php
class.amenity.php
class.category.php
所有四个类都处理不同类别的相应 CRUD 操作。我想重构我的代码,因此想使用父子结构。
例如,我曾经像这样初始化每个页面上的类。
$property = new Property($dbh);
$location = new Location($dbh);
$category = new Category($dbh);
$amenity = new Amenity($dbh);
然后我曾经像
$property->user;
$property->contact;
$property->save();
$location->countries();
$location-states();
等等一样单独访问类方法和属性,每个类都单独执行,而不是像这样访问它,我想以这种方式使用它。
$property = new Property($dbh)
上面应该是父类和其余三个子类,所以我应该只能通过父类访问所有类方法和属性,例如我应该只能像这样访问它..
$property->location->countries();
$property->locations->states();
$property->location->countryId;
$property->amenity->name;
$property->amenity->save();
等等..
我尝试过找出如何做到这一点并提出了这个解决方案。
class Property{
public $amenity;
public function __construct() {
require_once('class.amenity.php');
$this->amenity = new Amenity;
}
}
class Amenity {
public function create($test) {
return $test;
}
}
现在,如果我想访问 Amenity 类中的 create() 方法,我只需调用
$property->amenity->create()
它即可工作,但是我想知道这是否是实现父子结构的正确方法,或者我是否遗漏了某些内容?
I have the following PHP Classes
class.property.php
class.location.php
class.amenity.php
class.category.php
all four classes handles the respective CRUD operations for different categories. i want to refactor my codes and hence wants to go with the Parent Child Structure.
for example i used to initialize classes on every page like this.
$property = new Property($dbh);
$location = new Location($dbh);
$category = new Category($dbh);
$amenity = new Amenity($dbh);
and then i used to access class methods and properties individually like
$property->user;
$property->contact;
$property->save();
$location->countries();
$location-states();
Andso on, every class is executing indivdually, instead of accessing it like this i would like to use it this way.
$property = new Property($dbh)
above should be the Parent class and rest three child class, and so i should be able to access all class methods and properties only through parent class for example i should only be able to access it like this..
$property->location->countries();
$property->locations->states();
$property->location->countryId;
$property->amenity->name;
$property->amenity->save();
and so on..
i tried to figure out how to do it and came out with this solution.
class Property{
public $amenity;
public function __construct() {
require_once('class.amenity.php');
$this->amenity = new Amenity;
}
}
class Amenity {
public function create($test) {
return $test;
}
}
now if i want to access the create() method in Amenity class i simply call
$property->amenity->create()
and it works, however i would like to know if this is the correct method of implementing the Parent Child Structure or am i missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要
create()
调用:最多,您需要保护属性,并使用 getter 和 setter。
There is no need for the
create()
call:At the very most, you'll want to make the properties protected, and use getters and setters.