0

For some reason I can't get this to work:

<?php 
class Number{
    public $number;
    public $number_added;


    public function __construct(){
        $this->number_added = $this->add_two();
    }

    public function add_two(){
        return $this->number + 2;
    }
}   
?>

$this->number is set from Database, $this->number_two should be DB value + 2. However, when I echo $this->number_added, it returns two. The $number value was initialized correctly. This is a simplified example of my problem just to see if what I am trying to do possible? PHP OOP beginner.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Jursels
  • 173
  • 1
  • 3
  • 12

1 Answers1

3

You aren't setting the $number property anywhere prior to its use in add_two() (via the constructor), therefore PHP evaluates it as 0 during the addition.

You should pass in initial state during object construction, for example

public function __construct($number) {
    $this->number = $number;

    $this->number_added = $this->add_two();
}

Update

Allow me to illustrate the problem. Here's your current code and how I imagine you're using it

$number = 2;

$obj = new Number();
// right here, $obj->number is null (0 in a numeric sense)
// as your constructor calls add_two(), $obj->number_added is 2 (0 + 2)

$obj->number = $number;
// now $obj->number is 2 whilst $obj->number_added remains 2

Using my updated constructor, here is what happens

$number = 2;

$obj = new Number($number);
// $obj->number is set to $number (2) and a call to add_two() is made
// therefore $obj->number_added is 4
Phil
  • 157,677
  • 23
  • 242
  • 245
  • he said $number is set from the database, probably somewhere we can't see since it is a public variable. – David Harris Feb 05 '13 at 00:13
  • @DavidHarris He isn't setting it before the call to `add_two()` in the constructor though, therefore it is undefined. – Phil Feb 05 '13 at 00:14
  • oh, yeah -- I didn't really think about that lol. – David Harris Feb 05 '13 at 00:15
  • I am initializing $number straight from the DB result. If I echo it there is a value associated with it. – Jursels Feb 05 '13 at 00:16
  • @Jursels, what he is trying to say is that $number is still 0 until after `Number::add_two()` has finished. – David Harris Feb 05 '13 at 00:17
  • 1
    @Jursels When are you setting it? If it's after object creation (via `new Number()`), then it's too late to be used in the constructor – Phil Feb 05 '13 at 00:17
  • Phill, I create the object, then make a MYSQL query and instantiate the object by looping through the result and then I add the instatiated object to an array holding the object. – Jursels Feb 05 '13 at 00:21
  • @Jursels I've updated my answer with some explanations – Phil Feb 05 '13 at 00:25
  • I see what you mean Phill. __construct runs immediatly after the new Number(), so $number is empty. – Jursels Feb 05 '13 at 00:25
  • @Jursels No, the constructor does **not** run after `new Number()`, it **is** `new Number()`. Please read http://php.net/manual/en/language.oop5.decon.php – Phil Feb 05 '13 at 00:26
  • I see, so is there something else than constructor to use if I want to dynamically create a property from other properties in the same class? – Jursels Feb 05 '13 at 00:32