我有两个类; userFunction
和userDatabase
:通过类扩展的PHP类变量继承
class userDatabase {
protected $database = 'btc.db';
protected $short;
protected $salt;
function __construct(){
$this->short = $this->salt = bin2hex(random_bytes(6));
}
}
class userFunction extends userDatabase {
function __construct(){
$this->database.PHP_EOL;
$this->short.PHP_EOL;
$this->salt.PHP_EOL;
}
}
$r = new userFunction;
var_dump($r);
输出如下:
object(userFunction)#1 (3) {
["database":protected]=>
string(6) "btc.db"
["short":protected]=>
NULL
["salt":protected]=>
NULL
}
这不正是我所期待的。据推测我设置了$this->short
和$this->salt
从二进制数据生成一个随机的6字符随机十六进制盐。我扩展了userDatabase
类以继承变量到userFunction
,我希望能够通过中的$this->salt
和$this->short
调用userFunction
。但是,变量返回为NULL
。
我一直在寻找答案,为什么这是,但我似乎无法正确制定查询,因为我真的不知道这里发生了什么。 This seems to be a related issue, but I'm not entirely sure.具体来说,是否有可能完成我试图以这种特定方式做的事情? $this->salt
和$this->short
的每个实例在每个类中都是相同的,还是两者都不相同?我该如何解决我的NULL
问题,在这里?
我很感谢帮助。
什么是'$ this-> database.PHP_EOL'应该在做什么?它没有设置任何值,它不显示任何东西 –
至少做这个$ this-> short。= PHP_EOL; –
'$ this-> database'实际上是:'protected $ database ='myDatabase.db';'。所以这是我测试的一部分,以确保我可以访问变量作为预期的跨类,这似乎与这个特定的实例一起工作,而不是'$ this-> short'或'$ this-> salt'。 – zQueal