__construct($foo = NULL) is not working

261 Views Asked by At

I have strange problem. This is just constructor method of class, not worth to show other code, anyway, look at this code:

Class xy {
    public $x = 10;
    public $y = 10; 

    public function __construct($x = NULL, $y = NULL) {
        if(isset($x) || isset($y)){
            $this->x = $x;      // assign center coords
            $this->y = $y;      // assign center coords
        }
        $this->area = $this->area();
        echo $this->x . " " . $this->y . " " . $this->area;
    }

}

Since now, I thought that this code should echo $this->x and $this->y WITHOUT if(isset($x) || isset($y)){, if its not passed in on object making with this code: $newObj = new xy; But it doesn't. It works only if this line looks like this $newObj = new xy(10,10);

I need help and clarification :)

2

There are 2 best solutions below

1
On

I haven`t use php for years, but I think it should looks like $newObj = new xy();

0
On
change $newObj = xy(10,10); to $newObj = new xy(10,10);