简体   繁体   English

Class 在不声明变量的情况下工作?

[英]Class works without declaring variables?

I'm learned php as functional and procedure language.我学习了 php 作为功能和过程语言。 Right now try to start learn objective-oriented and got an important question.现在尝试开始学习面向目标并得到一个重要的问题。

I have code:我有代码:

class car {

    function set_car($model) {
        $this->model = $model;
    }

    function check_model()
    {
        if($this->model == "Mercedes") echo "Good car";
    }

}

$mycar = new car;
$mycar->set_car("Mercedes");

echo $mycar->check_model();

Why it does work without declaration of $model?为什么它在没有 $model 声明的情况下也能工作?

var $model; in the begin?一开始?

Because in php works "auto-declaration" for any variables?因为在 php 中,任何变量都可以“自动声明”? I'm stuck我卡住了

Every object in PHP can get members w/o declaring them: PHP 中的每个 object 都可以获得成员 w/o 声明:

$mycar = new car;
$mycar->model = "Mercedes";
echo $mycar->check_model(); # Good car

That's PHP's default behaviour.这是 PHP 的默认行为。 Those are public.那些是公开的。 See manual .参见手册

Yes, if it doesn't exist, PHP declares it on the fly for you.是的,如果它不存在,PHP 会即时为您声明它。

It is more elegant to define it anyway, and when working with extends it's recommended, because you can get weird situations if your extends are gonna use the same varnames and also don't define it private, protected or public.无论如何定义它更优雅,并且在使用 extends 时推荐它,因为如果你的 extends 将使用相同的 varnames 并且不将其定义为 private,protected 或 public,你可能会遇到奇怪的情况。

More info: http://www.php.net/manual/en/language.oop5.visibility.php更多信息: http://www.php.net/manual/en/language.oop5.visibility.php

PHP class members can be created at any time. PHP class 会员可随时创建。 In this way it will be treated as public variable.这样它将被视为公共变量。 To declare a private variable you need to declare it.要声明私有变量,您需要声明它。

Yes.是的。 But this way variables will be public.但是这样变量将是公开的。 And declaration class variable as "var" is deprecated - use public, protected or private.并且将 class 变量声明为“var”已弃用 - 使用公共、受保护或私有。

No, it's because $model is an argument of the function set_car.不,这是因为 $model 是 function set_car 的参数。 Arguments are not exactly variables, but placeholders (references) to the variables or values that will be set when calling the function (or class method). Arguments 不完全是变量,而是在调用 function(或 class 方法)时将设置的变量或值的占位符(引用)。 Eg, $model takes the value "Mercedes" when calling set_car.例如,$model 在调用 set_car 时取值“Mercedes”。

I think this behavior can lead to errors.我认为这种行为会导致错误。 Lets consider this code with one misprint让我们考虑一下这个代码有一个印刷错误

declare(strict_types=1);

class A
{
    public float $sum;
    public function calcSum(float $a, float $b): float
    {
        $this->sum = $a;
        $this->sums = $a + $b; //misprinted sums instead of sum
        return $this->sum;
    }
}

echo (new A())->calcSum(1, 1); //prints 1

Even I use PHP 7.4+ type hints and so one, neither compiler, nor IDE with code checkers can't find this typo.即使我使用 PHP 7.4+ 类型提示等等,编译器和代码检查器 IDE 都找不到这个错字。

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

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