首页 > 解决方案 > 那个简单的 php 类有什么问题?

问题描述

这里出了点问题,我在发什么消息?为什么width4的输出不是4?

输出:宽度1:4 宽度2:4 宽度3:4 宽度4:20

预期:宽度1:4 宽度2:4 宽度3:4 宽度4: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;
    }
}

?>

标签: phpoop

解决方案


错误的

$this->$int_width

真的

$this->int_width

推荐阅读