PHP中如何仅在需要时加载变量

发布于 2024-10-05 06:34:12 字数 642 浏览 8 评论 0原文

我有一个具有多个公共属性的类,其对象在系统的不同部分中使用。问题是,我只需要在使用类对象的每个位置加载其中一些公共属性,因为每次加载整个属性列表都将花费很长时间。

有什么方法可以使用 __autoload 或类似的函数来调用在调用时加载不同变量的函数吗?

例如

class Bread {
  public 
    $Ingredients,
    $Price,
    $Color;

  public function magicLoading($var) {
    switch($var) {
      case "Ingredients" : return loadIngredients();
      case "Price"       : return loadPrice();
      case "Color"       : return loadColor();
      default            : break;
    }
  }

  public function loadIngredients() {
    $this->Ingredients = ...
  }
}

foreach($Bread->Ingredients as $Ingredient) {
  //do stuff here
}

I have a class with multiple public properties whose objects are being used in different parts of the system. The problem is that I need to load only some of those public properties in each place I'm using the objects of the class, because loading the entire list of properties every time would take forever.

Is there any way I can use __autoload or a similar function to call the functions that load different variables at the time they are called?

E.g.

class Bread {
  public 
    $Ingredients,
    $Price,
    $Color;

  public function magicLoading($var) {
    switch($var) {
      case "Ingredients" : return loadIngredients();
      case "Price"       : return loadPrice();
      case "Color"       : return loadColor();
      default            : break;
    }
  }

  public function loadIngredients() {
    $this->Ingredients = ...
  }
}

foreach($Bread->Ingredients as $Ingredient) {
  //do stuff here
}

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

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

发布评论

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

评论(3

何处潇湘 2024-10-12 06:34:12

在您的代码中,

  • 将函数 magicLoading 重命名为 __get
  • 在“load...”方法中添加 return 语句,
  • 检查变量是否具有尚未初始化。

它有效!

In your code,

  • rename the function magicLoading to __get,
  • add a return statement in your 'load...' methods
  • check that the variables have not been initialized yet.

And it works !

樱花落人离去 2024-10-12 06:34:12

向我的同事道歉,因为他们发布了看似重复的答案,但他们错过了一些东西。

为了使 __get()__set() 挂钩正常工作,所涉及的成员变量根本不能被声明 - 只是声明但未定义是不够的。

这意味着您必须删除您希望以这种方式访问​​的任何变量的声明(即 public $Ingredients; 等),

因此,您的类可能如下所示。

class Bread
{
  /* No variable declarations or the magic methods won't execute */

  protected function loadIngredients()
  {
    $this->Ingredients = ...
  }

  public function __get( $var )
  {
    switch( $var )
    {
      case "Ingredients" : $this->loadIngredients(); break;
      case "Price"       : $this->loadPrice(); break;
      case "Color"       : $this->loadColor(); break;
      default            : throw new Exception( "No case for $var" );
    }
    return $this->$var;
  }
}

Apologies to my colleagues for posting what looks like a duplicate answer, but they have missed something.

In order for the __get() and __set() hooks to function properly, the member variable in question cannot be declared at all - simply being declared but undefined isn't sufficient.

This means you'll have to remove the declaration of any variables you want to be accessible in this way (i.e., the public $Ingredients; etc)

Ergo, your class might then look like this.

class Bread
{
  /* No variable declarations or the magic methods won't execute */

  protected function loadIngredients()
  {
    $this->Ingredients = ...
  }

  public function __get( $var )
  {
    switch( $var )
    {
      case "Ingredients" : $this->loadIngredients(); break;
      case "Price"       : $this->loadPrice(); break;
      case "Color"       : $this->loadColor(); break;
      default            : throw new Exception( "No case for $var" );
    }
    return $this->$var;
  }
}
断爱 2024-10-12 06:34:12

是的,您可以创建神奇的方法 __get 每次读取类的属性(尚未初始化)时都会调用它。

Yes, you can create the magic method __get that is called every time a property (that is not yet initialized) of the class is read.

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