【发布时间】:2017-04-15 09:45:39
【问题描述】:
我有 3 个文件,其中 2 个文件实例化了我的 myclass 类。为了确定它们来自哪个文件,我想将一个变量传递给类,然后能够输出不同的内容。
<?php
// File 1
$file1variable = '1';
$go = new myclass( $file1variable );
// File 2
$file2variable = '2';
$go = new myclass( $file2variable );
// File3
class myclass {
public function __construct() {
$this->display();
}
public function display() {
if( $file1variable ) {
echo 'file1';
} elseif( $file2variable ) {
echo 'file2';
}
}
}
?>
我已经阅读了反射类和这篇文章PHP: How to instantiate a class with arguments from within another class,但似乎无法让它适用于我的场景。
我怎样才能做到这一点?
【问题讨论】:
-
将其传递给
__contruct($var)并将其写入私有变量。或者编写一个 setter 并在 构建之后调用它