动态子类生成 - PHP

发布于 2024-08-05 07:30:01 字数 473 浏览 3 评论 0原文

我有一个抽象的“对象”类,它提供基本的 CRUD 功能以及验证等。通常,我会使用 __autoload($name) 魔术函数来加载一个存在于其自己文件中的类,该类的名称与我的类相同希望懒加载。代码看起来像这样,正如你可以想象的那样,它变得相当重复。

final class bicycle extends object {
    public function __construct($id=null) {
      parent::__construct($id, __CLASS__);
    }
    public function __toString() {
      return($this->name);
    }
}

我的问题是我是否可以以某种方式动态生成这些类,这样我就不必一遍又一遍地创建相同的功能 - 从而减少开销和设计时间。 PHP5 是否支持此功能,或者我只是高估了 OO PHP 的功能?

谢谢!

I have an abstract "object" class that provides basic CRUD functionality along with validation, etc. Typically I would use the __autoload($name) magic function to load a class that would exist in its own file, named the same as the class I wish to lazy load. The code would look something like this, which as you can imagine becomes quite repetitive.

final class bicycle extends object {
    public function __construct($id=null) {
      parent::__construct($id, __CLASS__);
    }
    public function __toString() {
      return($this->name);
    }
}

My question is whether or not I can somehow dynamically generate these classes on the fly so I don't have to create the same functionality over and over - thus reducing overhead and design time. Does PHP5 even support this or am I simply overestimating the power of OO PHP?

Thanks!

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

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

发布评论

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

评论(3

羁绊已千年 2024-08-12 07:30:01

与其复制粘贴此内容,为什么不将 __construct 和 __toString 方法的代码放在 object 类的定义中?

像这样的事情应该做:

class object {
    public function __construct($id = null) {
        $this->name = get_class($this);
    }
    public function __toString() {
      return($this->name);
    }
    protected $name;
}

final class bicycle extends object {

}

并且,调用它:

$a = new bicycle();
var_dump($a);

你会得到:

object(bicycle)[1]
  protected 'name' => string 'bicycle' (length=7)

这意味着类 bicycle 的实例,名称为 property 的值正确。

无需复制粘贴任何代码——除了 bicycle 类本身的定义。

作为旁注,如果您确实想动态生成一个类,您可能可以使用如下内容:

$code = 'final class bicycle extends object {}';
eval($code);

您只需动态构造 $code 变量即可。

但是我强烈建议不要这样做

  • 您的 IDE 中不会有代码辅助,因为它看不到该类,
  • 您的类不会有 phpdoc(同样的原因)
  • 总有“评估是邪恶的”之类的东西——这是千真万确的,至少在这种情况下是这样。
  • 在没有声明类的情况下使用“new bike”感觉是错误的!
  • 使用 eval 肯定会对性能产生一些影响,

声明一个新类并不是那么痛苦,而且我肯定更喜欢复制粘贴修改几行而不是使用这样的东西。

Instead of copy-pasting this, why don't you just put the code of the __construct and __toString methods in the definition of your object class ?

Something like this should do :

class object {
    public function __construct($id = null) {
        $this->name = get_class($this);
    }
    public function __toString() {
      return($this->name);
    }
    protected $name;
}

final class bicycle extends object {

}

And, calling it :

$a = new bicycle();
var_dump($a);

You get :

object(bicycle)[1]
  protected 'name' => string 'bicycle' (length=7)

Which means an instance of class bicycle, with the name property at the right value.

No need to copy-paste any code -- except for the definition of the bicycle class itself.

As a sidenote, if you really want to generate a class dynamically, you can probably use something like this :

$code = 'final class bicycle extends object {}';
eval($code);

You just have to construct the $code variable dynamically.

But I would strongly advise against this :

  • you will not have code assist in your IDE, as it cannot see the class
  • you will not have phpdoc for your class (same reason)
  • there is always the "eval is evil" stuff -- and that's quite true, at least in this situation.
  • using "new bicycle" without having declared the class feels wrong !
  • there's gotta be some performance implication with the use of eval

Declaring a new class is not such a pain, and I would definitly prefer copy-pasting-modifying a few line than use anything like this.

筱武穆 2024-08-12 07:30:01

嗯,对于 __toString,你只需将它放在父类中即可。示例如下:

class BaseObject {
    public function __toString() {
        return $this->name;
    }
}

class bicycle extends BaseObject {
}

$b = new bicycle();
$b->name = 'foo';
echo $b;

我不太确定您想要通过重写构造函数来传递 CLASS 来实现什么目的。

Well, for __toString, you just put it in the parent class. Example follows:

class BaseObject {
    public function __toString() {
        return $this->name;
    }
}

class bicycle extends BaseObject {
}

$b = new bicycle();
$b->name = 'foo';
echo $b;

I'm not exactly sure what you're trying to accomplish with overriding the constructor to pass in CLASS.

怪我太投入 2024-08-12 07:30:01

PHP5 中不存在此功能。它可能在 PHP6 中可用,但由于还没有适用于 ubuntu 的软件包,我不会继续。

This functionality does not exist in PHP5. It may be available in PHP6, but since there is no package for ubuntu yet I will not proceed.

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