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.

5
  • 1
    What is $this->database.PHP_EOL supposed to be doing? It's not setting any value, it's not displaying anything Commented Jan 24, 2016 at 12:51
  • at least do this $this->short .= PHP_EOL; Commented Jan 24, 2016 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. Commented Jan 24, 2016 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. Commented Jan 24, 2016 at 13:00
  • @IMSop I realized the mistake. Commented Jan 24, 2016 at 13:03

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

yeah, I was going to say that .You have to call the parent constructor inside userFuction constructor.
I feel terrible about it, but adding parent::__construct(); seems to have worked perfectly. Thanks for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.