繁体   English   中英

这些PHP变量之间有什么区别

[英]What is the difference between these PHP variables

谁能告诉我两者之间的区别是什么?

class Test {
  public $var; // I know this can be accessed from outside...

  public function __construct($var) {
    $this->var = $var; // This 
    $this->new_var = $var; // And this ... ? this is only internal like I would write private $new_var; ?
  }

  public function echoVar() {
    echo $this->new_var;
  }
}

两者之间实际上并没有任何根本区别-如果您在PHP中(从类内部或外部)写入未声明的属性,它将动态创建一个新的公共属性。 因此,给出以下脚本:

<?php
class Test {
  public function __construct() {
    $this->foo = 'foo';
  }
}

$test = new Test;
echo $test->foo;

您将获得输出foo 参见https://eval.in/632326

简而言之,如果要隐藏属性,则需要将其显式声明为privateprotected

您也可以在类上实现魔术方法__get__set ,以更好地处理对读取或写入动态属性的调用。 有关更多信息,请参见有关过载的手册页。

我将添加注释行以指定每个变量。

class Test {
  public $var; // I know this can be accessed from outside...
  //Variable/property that can be accessed from outside the class like you mentioned.    

  public function __construct($var) {
    $this->var = $var; // This 
    //$this calls a non static method or property from a class, in this case the property `public $var`       

    $this->new_var = $var; // And this ... ? this is only internal like I would write private $new_var; ?
    //Creates a new public property in the Test class 
  }

  public function echoVar() {
    echo $this->new_var;
    //echo the property new_var from Test class.
  }
}

注释中的解释

class Test {
    public $var; // This is a member variable or attribute.
                 // Something that this class has access to 
                 // and any of its sub-children

    public function __construct($var) {
        $this->var = $var; //If you mean $this then it is
                           // the current instance of this class see below
                           //If you mean $var it is a the parameter that 
                           //is passed to the method. See below as well

        $this->new_var = $var; // update: this adds a public member to the object
                               // essentially another $var just not easily known.
                               // Not sure a good use for this except confusion and chaos.
                               // If it were just $new_var then it is a 
                               // scoped variable in this method
    }
}

$thisparameters示例

$testPony = new Test("pony"); //An instance of test with a parameter of pony
$testUnicorn = new Test("unicorn"); //An instance of test with a parameter of unicorn

echo $testPony->var . "<br>";
echo $testUnicorn->var . "<br>";

echo $testPony->new_var . "<br>";
echo $testUnicorn->new_var . "<br>";

echo "<pre>";
var_dump($testPony);

上面将输出:

pony
unicorn
I like pony
I like unicorn
object(Test)#49 (3) {
   ["var"]=>string(4) "pony"
   ["new_var"]=>string(11) "I like pony"
}

请注意,最终转储仅显示公共成员(如果有私有成员),其格式为(假设私有$ foo) ["foo":"Test":private]=>NULL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM