我什么时候应该在PHP类中声明变量?

时间:2022-09-20 10:39:19

I'm new to the OOP paradigm, so there's probably a simple explanation for this question...

我是OOP范例的新手,所以对这个问题可能有一个简单的解释......

Do you always need to declare public object-wide variables in a class? For example:

您是否总是需要在类中声明公共对象范围的变量?例如:

<?php

class TestClass
{
    var $declaredVar;

    function __construct()
    {
        $this->declaredVar = "I am a declared variable.";
        $this->undeclaredVar = "I wasn't declared, but I still work.";
    }

    function display()
    {
        echo $this->declaredVar . "<br />";
        echo $this->undeclaredVar;
        echo "<br /><br />"; 
    }
}

$test = new TestClass;
$test->display();

$test->declaredVar = "The declared variable was changed.";
$test->undeclaredVar = "The undeclared variable was changed.";

$test->display();

?>

In this code, even though $declaredVar is the only declared variable, $undeclaredVar is just as accessible and useable--it seems to act as if I had declared it as public.

在这段代码中,即使$ declaredVar是唯一声明的变量,$ undeclaredVar也是可访问和可用的 - 它似乎就好像我已将其声明为public。

If undeclared class variables are always accessible like that, what's the point of declaring them all up front?

如果未声明的类变量总是可以像这样访问,那么事先声明它们是什么意思呢?

6 个解决方案

#1


35  

That variable isn't uninitialized, it's just undeclared.

这个变量不是未初始化的,它只是未声明的。

Declaring variables in a class definition is a point of style for readability. Plus you can set accessibility (private or public).

在类定义中声明变量是可读性的一种风格。另外,您可以设置辅助功能(私人或公共)。

Anyway, declaring variables explicitly has nothing to do with OOP, it's programming-language-specific. In Java you can't do that because variables must be declared explicitly.

无论如何,明确声明变量与OOP无关,它是特定于编程语言的。在Java中,您不能这样做,因为必须显式声明变量。

#2


13  

If you declare a member inside the class you can set its accessibility e.g

如果在类中声明成员,则可以设置其可访问性,例如

private $varname;

#3


9  

You should always declare your member variables and specify their accessibility within your classes. I like to put this information at the end of the class after my functions.

您应该始终声明您的成员变量并在您的类中指定它们的可访问性。我希望在我的功能之后将这些信息放在课程的最后。

You should define them as soon as you have enough information to do so. Possibly in the constructor or via setter functions.

一旦有足够的信息,您应该立即定义它们。可能在构造函数中或通过setter函数。

It is important to do this because it makes life much easier for people working with your code. They don't have to guess where different properties are coming from or why they're there. Also, most (if not all) IDEs will not pick up on class variables unless you've declared them somewhere. Code completion/hints are one of the many benefits of IDEs and without declaring your variables, you will render that functionality useless.

这样做很重要,因为它使使用代码的人的生活更加轻松。他们不必猜测不同属性的来源或原因。此外,大多数(如果不是全部)IDE都不会接受类变量,除非你已经在某个地方声明了它们。代码完成/提示是IDE的众多优点之一,如果不声明变量,您将无法使用该功能。

#4


1  

General OOP paradigm of encapsulation says you should not expose your inner state variables out side that means they should be private, that allows you to change an implementation of your class without need to change the code where you make use of it. It's better practice to initialize variables via constructors and getters and setters method of the class.

封装的一般OOP范例说你不应该暴露你的内部状态变量,这意味着它们应该是私有的,这允许你改变你的类的实现,而不需要改变你使用它的代码。最好通过类的构造函数和getter以及setter方法初始化变量。

#5


0  

In general variables should be initialized as soon as you have enough info to do it properly.

一般情况下,只要您有足够的信息正确执行变量,就应该初始化变量。

If a class variable needs certain info to be sensibly initialized then that info should be passed to the constructor.

如果一个类变量需要明确初始化某些信息,那么该信息应该传递给构造函数。

Using PHP's syntax to implicitly declare variables at the point of definition is, IMHO a surefire way to introduce bugs - if your class needs a variable then declare it, and use all of the information hiding that OOP affords you.

使用PHP的语法在定义点隐式声明变量是,恕我直言,一种引入错误的可靠方法 - 如果你的类需要一个变量然后声明它,并使用隐藏OOP提供给你的所有信息。

#6


0  

As Federico Culloca said "That variable isn't uninitialized, it's just undeclared". Also you didn't define any access modifiers for them so that they behaving like public modifier applied to them.

正如Federico Culloca所说:“这个变量不是未初始化的,它只是未宣布的”。此外,您没有为它们定义任何访问修饰符,以便它们的行为类似于应用于它们的公共修饰符。

You may already have known. PHP is a loosely typed language. But a programmer should always follow the best practices (but not to use primitive data types in php).

你可能已经知道了。 PHP是一种松散类型的语言。但是程序员应该始终遵循最佳实践(但不要在php中使用原始数据类型)。

You should use private modifier for class level variables and provide accessor and mutator methods (Getters and Setters) for them.

您应该对类级变量使用private修饰符,并为它们提供访问器和更改器方法(Getters和Setter)。

#1


35  

That variable isn't uninitialized, it's just undeclared.

这个变量不是未初始化的,它只是未声明的。

Declaring variables in a class definition is a point of style for readability. Plus you can set accessibility (private or public).

在类定义中声明变量是可读性的一种风格。另外,您可以设置辅助功能(私人或公共)。

Anyway, declaring variables explicitly has nothing to do with OOP, it's programming-language-specific. In Java you can't do that because variables must be declared explicitly.

无论如何,明确声明变量与OOP无关,它是特定于编程语言的。在Java中,您不能这样做,因为必须显式声明变量。

#2


13  

If you declare a member inside the class you can set its accessibility e.g

如果在类中声明成员,则可以设置其可访问性,例如

private $varname;

#3


9  

You should always declare your member variables and specify their accessibility within your classes. I like to put this information at the end of the class after my functions.

您应该始终声明您的成员变量并在您的类中指定它们的可访问性。我希望在我的功能之后将这些信息放在课程的最后。

You should define them as soon as you have enough information to do so. Possibly in the constructor or via setter functions.

一旦有足够的信息,您应该立即定义它们。可能在构造函数中或通过setter函数。

It is important to do this because it makes life much easier for people working with your code. They don't have to guess where different properties are coming from or why they're there. Also, most (if not all) IDEs will not pick up on class variables unless you've declared them somewhere. Code completion/hints are one of the many benefits of IDEs and without declaring your variables, you will render that functionality useless.

这样做很重要,因为它使使用代码的人的生活更加轻松。他们不必猜测不同属性的来源或原因。此外,大多数(如果不是全部)IDE都不会接受类变量,除非你已经在某个地方声明了它们。代码完成/提示是IDE的众多优点之一,如果不声明变量,您将无法使用该功能。

#4


1  

General OOP paradigm of encapsulation says you should not expose your inner state variables out side that means they should be private, that allows you to change an implementation of your class without need to change the code where you make use of it. It's better practice to initialize variables via constructors and getters and setters method of the class.

封装的一般OOP范例说你不应该暴露你的内部状态变量,这意味着它们应该是私有的,这允许你改变你的类的实现,而不需要改变你使用它的代码。最好通过类的构造函数和getter以及setter方法初始化变量。

#5


0  

In general variables should be initialized as soon as you have enough info to do it properly.

一般情况下,只要您有足够的信息正确执行变量,就应该初始化变量。

If a class variable needs certain info to be sensibly initialized then that info should be passed to the constructor.

如果一个类变量需要明确初始化某些信息,那么该信息应该传递给构造函数。

Using PHP's syntax to implicitly declare variables at the point of definition is, IMHO a surefire way to introduce bugs - if your class needs a variable then declare it, and use all of the information hiding that OOP affords you.

使用PHP的语法在定义点隐式声明变量是,恕我直言,一种引入错误的可靠方法 - 如果你的类需要一个变量然后声明它,并使用隐藏OOP提供给你的所有信息。

#6


0  

As Federico Culloca said "That variable isn't uninitialized, it's just undeclared". Also you didn't define any access modifiers for them so that they behaving like public modifier applied to them.

正如Federico Culloca所说:“这个变量不是未初始化的,它只是未宣布的”。此外,您没有为它们定义任何访问修饰符,以便它们的行为类似于应用于它们的公共修饰符。

You may already have known. PHP is a loosely typed language. But a programmer should always follow the best practices (but not to use primitive data types in php).

你可能已经知道了。 PHP是一种松散类型的语言。但是程序员应该始终遵循最佳实践(但不要在php中使用原始数据类型)。

You should use private modifier for class level variables and provide accessor and mutator methods (Getters and Setters) for them.

您应该对类级变量使用private修饰符,并为它们提供访问器和更改器方法(Getters和Setter)。