doctrine2 和 codeigniter2,父类与子属性交互的正确方法

发布于 2024-10-10 15:13:56 字数 1334 浏览 2 评论 0原文

我现在正在寻找这个有一段时间了。 我看到了一些类似的问题,但不确定它们是否适用于doctrine2和这个问题。 我正在尝试开始使用 CodeIgniter2 和 Doctrine2,但遇到了一些问题,我不是 php 中的 OOP 大师。 我希望 MainObject 包含 CRUD 和其他一些方法,并且我希望对象扩展该类并使用父级的方法,但使用子级的属性。由于 Doctrine2 使用对象的私有属性,因此我无法从父类访问这些方法。例如:

   Class MainObject{
    public function validateFields(){
        //go over each field and validate/update if needed
    }
    public function store(){
        // do some validation, some logic, store, etc...
    }
   }
   Class User extends MainObject{
    /**
        * @Column(type="string", length=32, unique=true, nullable=false)
        */
        private $username;

        /**
         * @Column(type="string", length=64, nullable=true)
         */
        private $email;

   }

现在,我想打电话给我

$user = new User();
//set properties, somehow
$user-__set($something);
//call validation or store or some method from the parent that will interact with child properties
$user->validate();
$user->store();
$user->uploadImage();
$user->formatPropertiesForSomethingSpecial();

,我几乎可以使用 ReflectionClass 来做到这一点,但我不确定我是否以正确的方式这样做? 或者有没有一种方法可以让 MainObject 具有所有这些方法,然后将 User 传递给它,以便它可以对用户执行应该执行的操作,这也许是“更正确”的方法?我不认为它允许我对用户的私有属性执行某些操作,但我想 User 可以拥有自己的 getter 和 setter?

我在 php 中使用 semi-oop 已经很多年了,但这对我来说是新的东西,所以感谢任何建议、教程、url、批评等。

I'm searching for this for quite some time now.
I saw few similar questions but not sure if they apply with doctrine2 and this problem.
I'm trying to get started with CodeIgniter2 and Doctrine2, and I'm having some problems, I'm not OOP guru in php.
I would like to have MainObject that will contains methods for CRUD and few others, and I want objects to extend that class and use parent's methods, but with child's properties. Since Doctrine2 uses Private Properties for objects, I cannot access these methods from parent class. For example:

   Class MainObject{
    public function validateFields(){
        //go over each field and validate/update if needed
    }
    public function store(){
        // do some validation, some logic, store, etc...
    }
   }
   Class User extends MainObject{
    /**
        * @Column(type="string", length=32, unique=true, nullable=false)
        */
        private $username;

        /**
         * @Column(type="string", length=64, nullable=true)
         */
        private $email;

   }

Now, I would like to just call

$user = new User();
//set properties, somehow
$user-__set($something);
//call validation or store or some method from the parent that will interact with child properties
$user->validate();
$user->store();
$user->uploadImage();
$user->formatPropertiesForSomethingSpecial();

I'm almost there to do this by using the ReflectionClass but I'm not sure that I'm doing this the right way?
Or is there a way to have MainObject with all this methods, and then just pass User to it so it can do what it should do with user, is perhaps that the 'righter' approach? I don't think it will allow me to do something with Private properties from the user, but I guess that User can have its own getters and setters?

I'm working with semi-oop in php for years, but this is something new for me, so thanks for any advice, tutorial, url, critique, anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

-黛色若梦 2024-10-17 15:13:56

最简单(也是正确)的方法是使用 setter。

Class User extends MainObject{
    private $username;
    private $email;
    public function set_username($username) {
        $this->username = $username;
    }
}

如果您想让它不那么冗长,请在父级中使用通用设置器:

Class MainObject{
公共函数 validateFields(){
}
公共函数存储(){
}
公共函数集($数据){
foreach ($data as $key => $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
}
}
}
}
// 例子
$用户=新用户();
$data = array('用户名' => 'foo', 'email' => '[电子邮件受保护]');
$用户->设置($数据);
$用户->validate();
$user->store();

对于第二种方法,您至少需要 PHP >= 5.1。

我刚刚温习了我的可见性文档。您需要使用受保护的属性而不是私有属性,或者在子类中编写所有设置器。

The easiest (and correct) way is to use a setter.

Class User extends MainObject{
    private $username;
    private $email;
    public function set_username($username) {
        $this->username = $username;
    }
}

If you want to make it less verbose, use a general purpose setter in the parent:

Class MainObject{
public function validateFields(){
}
public function store(){
}
public function set($data) {
foreach ($data as $key => $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
}
}
}
}
// Example
$user = new User();
$data = array('username' => 'foo', 'email' => '[email protected]');
$user->set($data);
$user->validate();
$user->store();

You will need at least PHP >= 5.1 for the second method.

I just brushed up on my visibility documentation. You'll need to use a protected property instead of private, or write all of your setters in the child class.

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