PHP - 在 class 中访问非 class 函数
PHP - accessing a non-class function inside a class
我的尝试: 在名为 bradpitt.php 的文件中有 $getSomeData
函数。它是一个简单的功能。这不在 class 里面。我有另一个文件名 jolie.php。此文件有 class。我试图访问该文件中的 $getSomeData()
的位置。
CoolPlugin.php
class CoolPlugin extends plugin
{
const COOLLIST = 'properties/coolBoy.json';
public function getSomeData () {
return DataUtil::readDataFile(self::COOLLIST);
}
bradpitt.php(非 Class 文件 - 一个简单的函数)
$getSomeData = function(){
$plugin = new \simulator\CoolPlugin();
return $plugin->getSomeData();
};
jolie.php
include_once 'bradpitt.php';
class Jolie{
public $getSomeData;
public function __construct(){
global $getSomeData;
$this->$getSomeData();
}
}
output.php
include_once 'jolie.php';
$joiliePage = new Jolie();
var_dump($joiliePage->getSomeData);
ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**
如何在 PHP 中的另一个 class 中调用和访问一个简单函数(将 return 作为对象)?
我在 return 为 NULL 的地方做错了什么?
您发布的代码充满了问题。
var_dump($joiliePage->getSomeData);
ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**
假设第 173 行是上面列出的那行,两条错误消息都说明了同一件事:变量 $joiliePage
未初始化(解释器认为它的值为 NULL
)。
不要被 PHP class 将它们定义为 "Notices" 这一事实所愚弄。从解释器的角度来看,它们是通知(它找不到变量),但它们是您的代码的错误,因为它无法成功继续。
include_once 'bradpitt.php';
class Jolie{
public $getSomeData;
public function __contruct(){
global $getSomeData;
$this->$getSomeData()
}
}
该函数称为 __contruct()
,但您可能希望它是 class' constructor。它不是构造函数,也不会被解释器自动调用,因为它没有正确的名称。构造函数的名称是 __construct()
。请注意,您的代码中缺少中间的 "s"。
方法__contruct()
声明了全局变量$getSomeData
。 如果 文件 bradpitt.php
被成功包含(如果文件不存在,它可能会失败并警告而不破坏脚本)那么 $getSomeData
符号指的是文件 bradpitt.php
.
中定义的同名变量
然而,调用 $this->$getSomeData()
并没有引用这个全局变量。它使用同名的 class' 属性 进行初始化。此代码不会 运行.
为了调用存储在全局变量$getSomeData
中的函数,代码应为:
public function __construct(){
global $getSomeData;
$getSomeData();
}
另请注意,该语句末尾缺少分号并产生语法错误。您的 class' 定义不正确,无法编译并且无法创建 Jolie
类型的对象。
我的尝试: 在名为 bradpitt.php 的文件中有 $getSomeData
函数。它是一个简单的功能。这不在 class 里面。我有另一个文件名 jolie.php。此文件有 class。我试图访问该文件中的 $getSomeData()
的位置。
CoolPlugin.php
class CoolPlugin extends plugin
{
const COOLLIST = 'properties/coolBoy.json';
public function getSomeData () {
return DataUtil::readDataFile(self::COOLLIST);
}
bradpitt.php(非 Class 文件 - 一个简单的函数)
$getSomeData = function(){
$plugin = new \simulator\CoolPlugin();
return $plugin->getSomeData();
};
jolie.php
include_once 'bradpitt.php';
class Jolie{
public $getSomeData;
public function __construct(){
global $getSomeData;
$this->$getSomeData();
}
}
output.php
include_once 'jolie.php';
$joiliePage = new Jolie();
var_dump($joiliePage->getSomeData);
ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**
如何在 PHP 中的另一个 class 中调用和访问一个简单函数(将 return 作为对象)?
我在 return 为 NULL 的地方做错了什么?
您发布的代码充满了问题。
var_dump($joiliePage->getSomeData);
ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**
假设第 173 行是上面列出的那行,两条错误消息都说明了同一件事:变量 $joiliePage
未初始化(解释器认为它的值为 NULL
)。
不要被 PHP class 将它们定义为 "Notices" 这一事实所愚弄。从解释器的角度来看,它们是通知(它找不到变量),但它们是您的代码的错误,因为它无法成功继续。
include_once 'bradpitt.php';
class Jolie{
public $getSomeData;
public function __contruct(){
global $getSomeData;
$this->$getSomeData()
}
}
该函数称为 __contruct()
,但您可能希望它是 class' constructor。它不是构造函数,也不会被解释器自动调用,因为它没有正确的名称。构造函数的名称是 __construct()
。请注意,您的代码中缺少中间的 "s"。
方法__contruct()
声明了全局变量$getSomeData
。 如果 文件 bradpitt.php
被成功包含(如果文件不存在,它可能会失败并警告而不破坏脚本)那么 $getSomeData
符号指的是文件 bradpitt.php
.
然而,调用 $this->$getSomeData()
并没有引用这个全局变量。它使用同名的 class' 属性 进行初始化。此代码不会 运行.
为了调用存储在全局变量$getSomeData
中的函数,代码应为:
public function __construct(){
global $getSomeData;
$getSomeData();
}
另请注意,该语句末尾缺少分号并产生语法错误。您的 class' 定义不正确,无法编译并且无法创建 Jolie
类型的对象。