将 PHP 类构造为父子类的正确方法是什么?

发布于 2024-11-08 18:54:43 字数 1375 浏览 3 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2024-11-15 18:54:43

不需要 create() 调用:

class Property{
    public $amenity;
    public function __construct() {
        require_once('class.amenity.php');
        $this->amenity = new Amenity;
    }
}

class Amenity {
}

$property = new Property;
$amenity = $property->amenity;

最多,您需要保护属性,并使用 getter 和 setter。

There is no need for the create() call:

class Property{
    public $amenity;
    public function __construct() {
        require_once('class.amenity.php');
        $this->amenity = new Amenity;
    }
}

class Amenity {
}

$property = new Property;
$amenity = $property->amenity;

At the very most, you'll want to make the properties protected, and use getters and setters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文