实例化类并访问不同类的方法
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过
Class_B::person()
您可以静态调用该方法。因此,您应该将person()
方法声明为静态方法,并且不能使用$this
,因为您没有Class_B
的实例。如果您需要
Class_B
的实例,只需在构造时创建它并存储在class_B
中即可。By
Class_B::person()
you are calling the method statically. So you should declare theperson()
method as static and can't use$this
because you don't have an instance ofClass_B
.If you need an instance of
Class_B
, just create it and store in theclass_B
on construction.不要将
require
放入方法内。include
d 文件中的代码继承了包含它的位置的范围,在您的示例中,会发生不好的事情。另外,对于类定义脚本,请考虑使用require_once
而不是require
,以避免多个定义。根据经验,请将所有
include
和require
放在脚本的顶部。更好的是,设置一个类自动加载器,并将其注册到auto_prepend
脚本中。这样,您就不必手动包含任何内容(至少对于类定义而言)。Don't put
require
inside a method. Code in aninclude
d file inherits the scope of the place where you include it, and in your example, bad things happen. Also, for class definition scripts, considerrequire_once
instead ofrequire
, to avoid multiple definitions.As a rule of thumb, put all
include
s andrequire
s at the top of your script. Better yet, set up a class autoloader, and register it in anauto_prepend
script. That way, you won't have to manually include anything at all (at least not for class definitions).您可能需要的是依赖项注入,即将
Class_B
的对象传递到Class_A
的构造函数中,并将其作为属性保存在Class_A
中。然后它在Class_A
中可用,如$this->classB
或类似的。What you might want is dependency injection, whereby you pass an object of
Class_B
intoClass_A
's constructor and hold it as a property inClass_A
. It then becomes available inClass_A
as$this->classB
or similar.