php如何在类函数中要求类?
我有一门课:php如何在类函数中要求类?,php,class,php-5.4,Php,Class,Php 5.4,我有一门课: class Test { public function someFunction() { require('SomeOtherClass.php'); } } 如果另一个类在物理上不能在一个类的函数中,我无法理解php在这里如何需要这个类?php是如何做到这一点的?php将类放在哪里?这是非常有效的代码: function foo() { class Foobar { function speak() {
class Test {
public function someFunction()
{
require('SomeOtherClass.php');
}
}
如果另一个类在物理上不能在一个类的函数中,我无法理解php在这里如何需要这个类?php是如何做到这一点的?php将类放在哪里?这是非常有效的代码:
function foo() {
class Foobar {
function speak() {
echo "Hi";
}
}
$obj = new Foobar();
$obj->speak();
}
// $obj = new Foobar(); // Will fail, as Foobar will be defined the global scope when running foo();
foo();
$obj = new Foobar(); // Will be OK, now the class is defined in the global scope
//foo(); // Fatal error: Cannot redeclare class Foobar
输出为:
Hi
有关详细信息,请参阅。这是完全有效的代码:
function foo() {
class Foobar {
function speak() {
echo "Hi";
}
}
$obj = new Foobar();
$obj->speak();
}
// $obj = new Foobar(); // Will fail, as Foobar will be defined the global scope when running foo();
foo();
$obj = new Foobar(); // Will be OK, now the class is defined in the global scope
//foo(); // Fatal error: Cannot redeclare class Foobar
输出为:
Hi
有关详细信息,请参阅。这是完全有效的代码:
function foo() {
class Foobar {
function speak() {
echo "Hi";
}
}
$obj = new Foobar();
$obj->speak();
}
// $obj = new Foobar(); // Will fail, as Foobar will be defined the global scope when running foo();
foo();
$obj = new Foobar(); // Will be OK, now the class is defined in the global scope
//foo(); // Fatal error: Cannot redeclare class Foobar
输出为:
Hi
有关详细信息,请参阅。这是完全有效的代码:
function foo() {
class Foobar {
function speak() {
echo "Hi";
}
}
$obj = new Foobar();
$obj->speak();
}
// $obj = new Foobar(); // Will fail, as Foobar will be defined the global scope when running foo();
foo();
$obj = new Foobar(); // Will be OK, now the class is defined in the global scope
//foo(); // Fatal error: Cannot redeclare class Foobar
输出为:
Hi
有关详细信息,请参见。在课程中使用以下内容:
class Test {
public $other_class;
function __construct() {
$this->other_class = new other_class();
}
public function someFunction() {
$this->other_class;
}
}
用这个。将您的课程设置为:
spl_autoload_register(function($class) {
include_once 'classes/'.$class.'.php';
});
在包含在所有位置的文件中使用该函数在类中使用以下命令:
class Test {
public $other_class;
function __construct() {
$this->other_class = new other_class();
}
public function someFunction() {
$this->other_class;
}
}
用这个。将您的课程设置为:
spl_autoload_register(function($class) {
include_once 'classes/'.$class.'.php';
});
在包含在所有位置的文件中使用该函数在类中使用以下命令:
class Test {
public $other_class;
function __construct() {
$this->other_class = new other_class();
}
public function someFunction() {
$this->other_class;
}
}
用这个。将您的课程设置为:
spl_autoload_register(function($class) {
include_once 'classes/'.$class.'.php';
});
在包含在所有位置的文件中使用该函数在类中使用以下命令:
class Test {
public $other_class;
function __construct() {
$this->other_class = new other_class();
}
public function someFunction() {
$this->other_class;
}
}
用这个。将您的课程设置为:
spl_autoload_register(function($class) {
include_once 'classes/'.$class.'.php';
});
在任何地方都包含的文件中使用该功能,这在以下文档中: 包含文件时,它包含的代码将继承包含文件所在行的变量范围。从该点开始,调用文件中该行可用的任何变量都将在被调用文件中可用但是,包含文件中定义的所有函数和类都具有全局作用域。(添加了强调)
require
与include
在几乎所有方面都是相同的,包括这个方面。这在以下文档中:
包含文件时,它包含的代码将继承包含文件所在行的变量范围。从该点开始,调用文件中该行可用的任何变量都将在被调用文件中可用但是,包含文件中定义的所有函数和类都具有全局作用域。(添加了强调)
require
与include
在几乎所有方面都是相同的,包括这个方面。这在以下文档中:
包含文件时,它包含的代码将继承包含文件所在行的变量范围。从该点开始,调用文件中该行可用的任何变量都将在被调用文件中可用但是,包含文件中定义的所有函数和类都具有全局作用域。(添加了强调)
require
与include
在几乎所有方面都是相同的,包括这个方面。这在以下文档中:
包含文件时,它包含的代码将继承包含文件所在行的变量范围。从该点开始,调用文件中该行可用的任何变量都将在被调用文件中可用但是,包含文件中定义的所有函数和类都具有全局作用域。(添加了强调)
require
在几乎所有方面都与include
相同,包括这个。为什么另一个类不能“物理地”位于类的函数中?@popla面我的意思是,你不能在代码编辑器的函数中定义类,不是吗?,因为我从来没有在函数中看到过一个类,它是关于范围的
,所以如果我在一个类函数中需要它,其他类会看到这个需要的类吗?是的,但只有在它被需要之后。为什么另一个类不能“物理地”看到它是否在类的函数中?@poplane我的意思是,您不能在代码编辑器中定义函数中的类,不是吗?因为我从未在函数中看到过类,这与作用域有关,所以如果我在类函数中需要它,其他类会看到这个需要的类吗?是的,但只有在它被需要之后。为什么另一个类不能“物理地”在一个类的函数中?@poplaines我的意思是你不能在代码编辑器的函数中定义一个类,不是吗?,因为我从来没有在函数中看到过一个类,它是关于范围的
,所以如果我在一个类函数中需要它,其他类会看到这个需要的类吗?是的,但只有在它被需要之后。为什么另一个类不能“物理地”看到它是否在类的函数中?@poplane我的意思是,您不能在代码编辑器中定义函数中的类,不是吗?因为我从未在函数中看到过类,这与作用域有关,所以如果我在类函数中需要它,其他类会看到这个必需的类吗?是的,但只有在它被必需之后。$obj=new\Foobar();运行foo()后,在PHP5.3上运行良好,因为所有类都是在“全局”或命名空间范围中定义的。这个问题并不像第一次出现的那样“直截了当”。它更多的是关于“编译时”和“运行时”。然后“编译”其他东西。谢谢你的注意,我在类上应用“范围规则”时也太匆忙了。。。不幸的是,事实并非如此。我修正了这个问题。$obj=new\Foobar();运行foo()后,在PHP5.3上运行良好,因为所有类都是在“全局”或命名空间范围中定义的。这个问题并不像第一次出现的那样“直截了当”。它更多的是关于“编译时”和“运行时”。然后“编译”其他东西。谢谢你的注意,我在类上应用“范围规则”时也太匆忙了。。。不幸的是,事实并非如此。我修正了这个问题。$obj=new\Foobar();运行foo()后,在PHP5.3上运行良好,因为所有类都是在“全局”或命名空间范围中定义的。这个问题并不像第一次出现的那样“直截了当”。它更多的是关于“编译时”和“运行时”。然后“编译”其他东西。谢谢你的注意,我在类上应用“范围规则”时也太匆忙了。。。不幸的是,事实并非如此。我修正了。$obj=new\Fo