Reputation: 15
I have two classes; userFunction
and userDatabase
:
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);
Output is as follows:
object(userFunction)#1 (3) {
["database":protected]=>
string(6) "btc.db"
["short":protected]=>
NULL
["salt":protected]=>
NULL
}
This isn't exactly what I was expecting. Presumably I set $this->short
and $this->salt
to generate a random 6 character random hex salt from binary data. I extended the userDatabase
class to inherit the variables to userFunction
which I expect to be able to call via $this->salt
and $this->short
within __construct()
of userFunction
. However, the variables are returned as NULL
.
I've been searching for an answer to why this is, but I don't seem to be able to formulate the query correctly as I'm honestly not sure what's happening here. This seems to be a related issue, but I'm not entirely sure. Specifically, is it even possible to accomplish what I'm trying to do this particular way? Is each instance of $this->salt
and $this->short
the same in each class, or are they both different? How would I fix my NULL
problem, here?
I appreciate the assistance.
Upvotes: 0
Views: 158
Reputation: 25954
When you override __construct
(or any other method) in a child, the parent's method doesn't get called - unless you explicitly do so.
class userFunction extends userDatabase {
function __construct(){
parent::__construct();
$this->database.PHP_EOL;
$this->short.PHP_EOL;
$this->salt.PHP_EOL;
}
}
Upvotes: 3