[英]How to include file & access class method from from within a method of another class?
I have a class called numDisplay
containing several methods. 我有一个名为numDisplay
的类, numDisplay
包含几种方法。 Most of those methods need to use a method from another class called prefs
which is contained in another file during the course of their duties. 这些方法中的大多数都需要使用另一个称为prefs
类中的方法,该方法在工作期间包含在另一个文件中。
I think I could include the file, instantiate the class prefs
and then access the method individually from inside each of the methods where it is needed, however, I expect there is a better way? 我想我可以包含文件,实例化类prefs
,然后从需要它的每个方法内部分别访问该方法,但是,我希望有更好的方法吗?
I thought of doing the file require_once
in the __constructor
of numDisplay
and instantiate the class there also, then I could just call it from the methods of numDisplay
. 我想到了在numDisplay
的__constructor
中执行文件require_once
并在那里实例化该类,然后我可以从numDisplay
的方法中调用它。
I tried the code below and various syntax variations, but can't get it to work. 我尝试了以下代码和各种语法变体,但无法正常工作。 How should I do this please? 我该怎么办?
class numDisplay {
private $P;
function __construct($P) {
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/classes/preferences.php');
$P = new Preferences();
}
public function displayBSNo($num, $invType) {
$auto_bs_no = $this->$P->getPreference('auto_bs_no');
$length_bs_no = $this->$P->getPreference('length_bs_no');
$length_alt_bs_no = $this->$P->getPreference('length_alt_bs_no');
if ($invType == 1) { // It's a standard BS
if ($auto_bs_no == '1') {
return "BS-" . $this->pad($num, $length_bs_no);
} else {
if ($num == '') {
return '<i>none</i>';
} else {
return $num;
}
}
}
elseif ($invType == 2) {
return "PI-" . $this->pad($num, $length_alt_bs_no);
}
}
}
When I attempt to use displayBSNo(), I get the following error: 当我尝试使用displayBSNo()时,出现以下错误:
Fatal error: Call to a member function getPreference() on a non-object in /home/peter/Documents/websites/Our_websites/bookkeeper.ph/books.bookkeeper.ph/public/includes/classes/common.classes.php on line 347
Instead of 代替
$this->$P->getPreference('')
use 采用
$this->P->getPreference('')
You're not setting the property in the constructor: 您没有在构造函数中设置属性:
function __construct($P) {
require_once($_SERVER['DOCUMENT_ROOT'].'/includes/classes/preferences.php');
$P = new Preferences();
}
Here, $P
is just a local variable in the scope of your constructor; 在这里, $P
只是构造函数范围内的局部变量。 to store the value inside the property P
, you should have: 要将值存储在属性P
,您应该具有:
$this->P = new Preferences();
In the rest of your code: 在其余的代码中:
$this->$P->getPreference(...)
References the property of which the name is held in $P
, which is probably not what you want. 引用名称保留在$P
的属性,这可能不是您想要的。 It should be: 它应该是:
$this->P->getPreference(...)
PHP actually emits notices when you make these kinds of mistakes; 当您犯此类错误时,PHP实际上会发出通知。 you can see them with this: 您可以通过以下方式看到它们:
error_reporting(-1);
ini_set('display_errors', 'On');
Use these settings during development only. 仅在开发期间使用这些设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.