php 设置公共类变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1981375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting public class variables
提问by Cudos
How do I set a public variable. Is this correct?:
如何设置公共变量。这样对吗?:
class Testclass
{
public $testvar = "default value";
function dosomething()
{
echo $this->testvar;
}
}
$Testclass = new Testclass();
$Testclass->testvar = "another value";
$Testclass->dosomething();
回答by marvin
this is the way, but i would suggest to write a getter and setter for that variable.
这就是方法,但我建议为该变量编写一个 getter 和 setter。
class Testclass
{
private $testvar = "default value";
public function setTestvar($testvar) {
$this->testvar = $testvar;
}
public function getTestvar() {
return $this->testvar;
}
function dosomething()
{
echo $this->getTestvar();
}
}
$Testclass = new Testclass();
$Testclass->setTestvar("another value");
$Testclass->dosomething();
回答by Srikar Doddi
Use Constructors.
使用构造函数。
<?php
class TestClass
{
public $testVar = "default value";
public function __construct($varValue)
{
$this->testVar = $varValue;
}
}
$object = new TestClass('another value');
print $object->testVar;
?>
回答by Gurunatha Dogi
class Testclass
{
public $testvar;
function dosomething()
{
echo $this->testvar;
}
}
$Testclass = new Testclass();
$Testclass->testvar = "another value";
$Testclass->dosomething(); ////It will print "another value"
回答by AntonioCS
If you are going to follow the examples given (using getter/setter or setting it in the constructor) change it to private since those are ways to control what is set in the variable.
如果您打算按照给出的示例(使用 getter/setter 或在构造函数中设置它)将其更改为私有,因为这些是控制变量中设置内容的方法。
It doesn't make sense to keep the property public with all those things added to the class.
将所有这些添加到类中的东西保持公开是没有意义的。
回答by ntd
Inside class Testclass:
内部class Testclass:
public function __construct($new_value)
{
$this->testvar = $new_value;
}
回答by Scott Saunders
You're "setting" the value of that variable/attribute. Not overriding or overloading it. Your code is very, very common and normal.
您正在“设置”该变量/属性的值。不要覆盖或重载它。你的代码非常非常常见和正常。
All of these terms ("set", "override", "overload") have specific meanings. Override and Overload are about polymorphism (subclassing).
所有这些术语(“set”、“override”、“overload”)都有特定的含义。Override 和 Overload 是关于多态性(子类化)的。
From http://en.wikipedia.org/wiki/Object-oriented_programming:
来自http://en.wikipedia.org/wiki/Object-oriented_programming:
Polymorphism allows the programmer to treat derived class members just like their parent class' members. More precisely, Polymorphism in object-oriented programming is the ability of objects belonging to different data types to respond to method calls of methods of the same name, each one according to an appropriate type-specific behavior. One method, or an operator such as +, -, or *, can be abstractly applied in many different situations. If a Dog is commanded to speak(), this may elicit a bark(). However, if a Pig is commanded to speak(), this may elicit an oink(). They both inherit speak() from Animal, but their derived class methods override the methods of the parent class; this is Overriding Polymorphism. Overloading Polymorphism is the use of one method signature, or one operator such as "+", to perform several different functions depending on the implementation. The "+" operator, for example, may be used to perform integer addition, float addition, list concatenation, or string concatenation. Any two subclasses of Number, such as Integer and Double, are expected to add together properly in an OOP language. The language must therefore overload the addition operator, "+", to work this way. This helps improve code readability. How this is implemented varies from language to language, but most OOP languages support at least some level of overloading polymorphism.
多态允许程序员像对待父类的成员一样对待派生类成员。更准确地说,面向对象编程中的多态性是属于不同数据类型的对象能够响应同名方法的方法调用,每个方法都根据适当的特定于类型的行为。一种方法或运算符(例如 +、- 或 *)可以抽象地应用于许多不同的情况。如果狗被命令说(),这可能会引起吠叫()。但是,如果命令 Pig 使用 speak(),这可能会引发 oink()。它们都继承了Animal的speak(),但是它们的派生类方法覆盖了父类的方法;这是覆盖多态性。重载多态是使用一个方法签名,或者一个操作符,比如“+”,根据实现来执行几个不同的功能。例如,“+”运算符可用于执行整数加法、浮点加法、列表连接或字符串连接。Number 的任何两个子类,例如 Integer 和 Double,都应该在 OOP 语言中正确地相加。因此,语言必须重载加法运算符“+”才能以这种方式工作。这有助于提高代码的可读性。这是如何实现的因语言而异,但大多数 OOP 语言至少支持一定程度的重载多态性。期望在 OOP 语言中正确地添加在一起。因此,语言必须重载加法运算符“+”才能以这种方式工作。这有助于提高代码的可读性。实现方式因语言而异,但大多数 OOP 语言至少支持一定程度的重载多态性。期望在 OOP 语言中正确地添加在一起。因此,语言必须重载加法运算符“+”才能以这种方式工作。这有助于提高代码的可读性。实现方式因语言而异,但大多数 OOP 语言至少支持一定程度的重载多态性。
回答by Wim
For overloading you'd need a subclass:
对于重载,您需要一个子类:
class ChildTestclass extends Testclass {
public $testvar = "newVal";
}
$obj = new ChildTestclass();
$obj->dosomething();
This code would echo newVal.
此代码将 echo newVal。
回答by Zaje
Add getter and setter method to your class.
将 getter 和 setter 方法添加到您的类中。
public function setValue($new_value)
{
$this->testvar = $new_value;
}
public function getValue()
{
return $this->testvar;
}

