实例化类并访问不同类的方法

发布于 2024-11-19 16:08:15 字数 994 浏览 0 评论 0原文

我正在尝试在 Class_A 的函数之一中使用 Class_B 函数。但是 Class_B 的许多函数都包含 $this ,这会导致问题。这些都是类(每个类当然都在自己的文件中):

class Class_A
{
   function hello()
   {
      require('class_b.php');
      echo 'Hello ' . Class_B::person();
   }

   function bye()
   {
      require('class_b.php');
      echo 'Bye ' . Class_B::person();
   }
}

class Class_B
{
   function person()
   {
      // various operations and variables
      echo $this->get_user($id);
   }
}

当我运行 Class_A 文件时,我得到 Call to undefined method Class_A::person() in (...) 因为我认为当我实例化 Class_A 类时,$this 值发生变化。它推翻了 Class_B 值。我怎样才能阻止这个?

另外,另一个问题:如何从 Class_A 中的每个函数访问 Class_B?我不想重新声明班级。我是否需要这样做:

class Class_A
{
   function function1()
   {
      require('class_b.php');
      $class_b = new Class_B();
      // code
   }

   function function2()
   {
      require('class_b.php');
      $class_b = new Class_B();
      // code
   }
}

或者我是否需要使用构造函数或其他东西?

I'm trying to use a Class_B function in one of Class_A's functions. But a lot of Class_B's functions include $this which causes problems. These are both classes (each class is of course in its own file):

class Class_A
{
   function hello()
   {
      require('class_b.php');
      echo 'Hello ' . Class_B::person();
   }

   function bye()
   {
      require('class_b.php');
      echo 'Bye ' . Class_B::person();
   }
}

class Class_B
{
   function person()
   {
      // various operations and variables
      echo $this->get_user($id);
   }
}

When I run the Class_A file I get Call to undefined method Class_A::person() in (...) because I think the $this value is changed when I instantiate the Class_A class. It overrules the Class_B value. How can I stop this?

Also, another question: how can I access Class_B from every function in Class_A? I don't want to redeclare the class. Do I need to do this:

class Class_A
{
   function function1()
   {
      require('class_b.php');
      $class_b = new Class_B();
      // code
   }

   function function2()
   {
      require('class_b.php');
      $class_b = new Class_B();
      // code
   }
}

Or do I need to use a constructor or something?

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

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

发布评论

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

评论(3

刘备忘录 2024-11-26 16:08:15

通过 Class_B::person() 您可以静态调用该方法。因此,您应该将 person() 方法声明为静态方法,并且不能使用 $this,因为您没有 Class_B 的实例。

如果您需要 Class_B 的实例,只需在构造时创建它并存储在 class_B 中即可。

class Class_A {
  private $b;
  function __construct()
  {
    $this->b = new Class_B();
  }     
  function stuff()
  {
    $this->b->person();
  }

By Class_B::person() you are calling the method statically. So you should declare the person() method as static and can't use $this because you don't have an instance of Class_B.

If you need an instance of Class_B, just create it and store in the class_B on construction.

class Class_A {
  private $b;
  function __construct()
  {
    $this->b = new Class_B();
  }     
  function stuff()
  {
    $this->b->person();
  }
So要识趣 2024-11-26 16:08:15

不要将 require 放入方法内。 included 文件中的代码继承了包含它的位置的范围,在您的示例中,会发生不好的事情。另外,对于类定义脚本,请考虑使用 require_once 而不是 require,以避免多个定义。

根据经验,请将所有 includerequire 放在脚本的顶部。更好的是,设置一个类自动加载器,并将其注册到 auto_prepend 脚本中。这样,您就不必手动包含任何内容(至少对于类定义而言)。

Don't put require inside a method. Code in an included file inherits the scope of the place where you include it, and in your example, bad things happen. Also, for class definition scripts, consider require_once instead of require, to avoid multiple definitions.

As a rule of thumb, put all includes and requires at the top of your script. Better yet, set up a class autoloader, and register it in an auto_prepend script. That way, you won't have to manually include anything at all (at least not for class definitions).

南渊 2024-11-26 16:08:15

您可能需要的是依赖项注入,即将 Class_B 的对象传递到 Class_A 的构造函数中,并将其作为属性保存在 Class_A 中。然后它在 Class_A 中可用,如 $this->classB 或类似的。

// Include Class_B code outside the class.
require_once('class_b.php');
class Class_A {
  // Property to hold Class_B
  public $b;

  // Constructor
  public function __construct($b) {
    $this->b = $b;
  }

  public function function1(){
     // code
     $this->b;
  }
  public function function2(){
     // code
     $this->b;
  }
}

// Now call your objects:
// The Class_B which will be injected into A
$b = new Class_B();
// Your Class_A, which holds an instance of Class_B as a property
$a = new A($b);

What you might want is dependency injection, whereby you pass an object of Class_B into Class_A's constructor and hold it as a property in Class_A. It then becomes available in Class_A as $this->classB or similar.

// Include Class_B code outside the class.
require_once('class_b.php');
class Class_A {
  // Property to hold Class_B
  public $b;

  // Constructor
  public function __construct($b) {
    $this->b = $b;
  }

  public function function1(){
     // code
     $this->b;
  }
  public function function2(){
     // code
     $this->b;
  }
}

// Now call your objects:
// The Class_B which will be injected into A
$b = new Class_B();
// Your Class_A, which holds an instance of Class_B as a property
$a = new A($b);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文