0

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.

Community
  • 1
  • 1
zQueal
  • 15
  • 2
  • 1
    What is `$this->database.PHP_EOL` supposed to be doing? It's not setting any value, it's not displaying anything – Mark Baker Jan 24 '16 at 12:51
  • at least do this $this->short .= PHP_EOL; – Mohammad Alabed Jan 24 '16 at 12:53
  • `$this->database` is actually: `protected $database = 'myDatabase.db';`. So it's part of my test to ensure that I can access the variables as intended cross class, which seems to be working with this particular instance, just not with `$this->short` or `$this->salt`. – zQueal Jan 24 '16 at 12:53
  • "I have two functions..." - no, you have two *classes*. It may seem picky, but learning the right terms will help you find and understand help so much easier. – IMSoP Jan 24 '16 at 13:00
  • @IMSop I realized the mistake. – zQueal Jan 24 '16 at 13:03

1 Answers1

3

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;
    }
}
roippi
  • 25,533
  • 4
  • 48
  • 73
  • yeah, I was going to say that .You have to call the parent constructor inside userFuction constructor. – Ricardo Silva Jan 24 '16 at 12:55
  • I feel terrible about it, but adding `parent::__construct();` seems to have worked perfectly. Thanks for your time! – zQueal Jan 24 '16 at 12:56