【问题标题】:without defining public property is accessible using $this in php OOP [duplicate]在 php OOP 中使用 $this 可以访问不定义公共属性 [重复]
【发布时间】:2019-05-17 19:49:41
【问题描述】:

我正在尝试做这样的事情:

<?php
class Circle{
    //public $radious=1;
    function SetRadius($param)
    {
        $this->radious=$param;
    }
    function AreaOfCircle(){
        return $this->radious * $this->radious * M_PI;
    }
}
$obj=new Circle();
$obj->SetRadius(5);
echo $obj->AreaOfCircle(); //78.539816339745
?>

我在类级别定义了一个属性: 但是当我评论这个(半径)属性时,程序运行正常。但根据 oop 概念 它应该返回错误:“未定义的属性”。但我知道它在没有定义属性的情况下运行良好。 谁能帮我解释它为什么有效?

【问题讨论】:

    标签: php oop php-7


    【解决方案1】:

    我明白了。

    PHP 类与其他语言中的类并不完全相同,其中成员变量需要指定为类声明的一部分。 PHP 类成员可以随时创建。

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多