Skip to content
Advertisement

What’s wrong with that simple php class? [closed]

Something is wrong here, what am I mssing? Why is the output at width4 not 4?

Output : width1: 4 width2: 4 width3: 4 width4: 20

Expected: width1: 4 width2: 4 width3: 4 width4: 4

<?php

$mytest = new MyTestClass(4, 5);

echo " width4: ".$mytest->$int_width;

class MyTestClass {

    public $int_width;
    public $int_height;
    public $int_area;

    public function __construct($int_width, $int_height){
        $this->my_new_test($int_width, $int_height);
        echo " width3: ".$this->$int_width;
    }

    function my_new_test($int_width, $int_height){

        $this->$int_width = $int_width;
        $this->$int_height = $int_height;

        echo " width1: ".$this->$int_width;

        $this->$int_area = $int_width * $int_height;

        echo " width2: ".$this->$int_width;
    }
}

?>

Advertisement

Answer

False

$this->$int_width

True

$this->int_width
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement