简体   繁体   English

在类中声明变量的目的是什么

[英]What is the purpose of declaring variables in a class

What is the purpose of declaring a variable at the top of a class if it works without it? 如果一个变量在没有它的情况下工作,那么在一个类的顶部声明一个变量的目的是什么?

class Testclass
{
    public $testvar = "default value";

    public function setTestvar($testvar) { 
        $this->testvar = $testvar; 
    }

    public function getTestvar() { 
        return $this->testvar; 
    }
}

compared to: 相比:

class Testclass
{
    public function setTestvar($testvar) { 
        $this->testvar = $testvar; 
    }

    public function getTestvar() { 
        return $this->testvar; 
    }
}

Both variants are correct but the second one is lacking when getting testvar before you initialize it. 两种变体都是正确的,但在初始化之前获取testvar时缺少第二种变体。

If you call $test->getTestvar(); 如果你调用$test->getTestvar(); before you set it with $test->setTestval('bla bla'); 在使用$test->setTestval('bla bla');设置它之前$test->setTestval('bla bla'); , you will get a warning, something like: ,你会得到一个警告,如:

Notice : Undefined property: Testclass::$testvar 注意 :未定义的属性:Testclass :: $ testvar

The second variant also lacks the property visibility part (ie private, protected). 第二个变体也缺少属性可见性部分(即私有,受保护)。 More about visibility . 更多关于可见性

The declaration of class properties above methods is a good practice, it's taken from strict oop-languages like java. 方法上面的类属性声明是一种很好的做法,它取自严格的oop语言,如java。

In PHP there is no functional purpose to including the declaration. 在PHP中,包含声明没有功能目的。 It seems that you have read up on the documentation and you are 100% correct. 您似乎已经阅读了文档并且100%正确。

There is one case however. 但是有一个案例。 If we call the getter before setting the variable, we will get an undefined variable exception. 如果我们 在设置变量之前 调用 getter ,我们将得到一个undefined variable异常。

Now let's think about this. 现在让我们考虑一下。

Let's say I am a programmer, specialized in c++ and java. 假设我是一名程序员,专门研究c ++和java。 I'm a pro at those languages (so I pick up PHP pretty quick), and your company just hired me to help work on the web application you are working on. 我是这些语言的专家(所以我很快就拿起PHP),而你的公司只是聘请我来帮助你处理你正在使用的Web应用程序。 I take a look at this object, and have no clue what's going on. 我看一下这个对象,并且不知道发生了什么。

Really what it comes down to is that PHP is one of the weirdest languages I know. 真正归结为PHP是我所知道的最奇怪的语言之一。 You have stumbled across just one of many odd things that PHP does. 你偶然发现了PHP所做的许多奇怪的事情之一。 This will not be your last. 这不会是你的最后一次。 Objects are templates; 对象是模板; they are pieces of code that programmers can use for a variety of reasons. 它们是程序员可以出于各种原因使用的代码片段。 The more information and formatting that we can do when making the object, the more effectively it can be used by other programmers. 在制作对象时我们可以做的信息和格式越多,其他程序员就越有效地使用它。 Its the exact same reason as to why we format our code. 它与我们格式化代码的原因完全相同。

TL;DR TL; DR

No reason, besides documentation, ease of reading, and avoiding Undeclared Variables. 没有理由,除了文档,易于阅读和避免未声明的变量。

Edit 编辑

Another important issue was brought up 提出了另一个重要问题

Declaring variables in the global context allows you to set your properties visibility. 在全局上下文中声明变量允许您设置属性可见性。 Public or Private. 公共或私人。 Because you have getters and setters I am assuming that your properties are private. 因为你有getter和setter我假设你的属性是私有的。 So you need to declare that. 所以你需要声明。

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

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