设置公共类变量

发布于 2024-08-16 22:45:54 字数 276 浏览 6 评论 0原文

如何设置公共变量。这是正确的吗?:

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

  function dosomething()
  {
    echo $this->testvar;
  }
}

$Testclass = new Testclass();
$Testclass->testvar = "another value";    
$Testclass->dosomething();

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

魔法少女 2024-08-23 22:45:54

就是这样,但我建议为该变量编写一个 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();

this is the way, but i would suggest to write a getter and setter for that variable.

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();
画骨成沙 2024-08-23 22:45:54

使用构造函数。

<?php
class TestClass
{
    public $testVar = "default value";
    public function __construct($varValue)
    {
       $this->testVar = $varValue;               
    }
}    
$object = new TestClass('another value');
print $object->testVar;
?>

Use Constructors.

<?php
class TestClass
{
    public $testVar = "default value";
    public function __construct($varValue)
    {
       $this->testVar = $varValue;               
    }
}    
$object = new TestClass('another value');
print $object->testVar;
?>
手长情犹 2024-08-23 22:45:54
class Testclass
{
  public $testvar;

  function dosomething()
  {
    echo $this->testvar;
  }
}

$Testclass = new Testclass();
$Testclass->testvar = "another value";    
$Testclass->dosomething(); ////It will print "another value"
class Testclass
{
  public $testvar;

  function dosomething()
  {
    echo $this->testvar;
  }
}

$Testclass = new Testclass();
$Testclass->testvar = "another value";    
$Testclass->dosomething(); ////It will print "another value"
稚然 2024-08-23 22:45:54

对于重载,您需要一个子类:

class ChildTestclass extends Testclass {
    public $testvar = "newVal";
}

$obj = new ChildTestclass();
$obj->dosomething();

此代码将回显 newVal

For overloading you'd need a subclass:

class ChildTestclass extends Testclass {
    public $testvar = "newVal";
}

$obj = new ChildTestclass();
$obj->dosomething();

This code would echo newVal.

那一片橙海, 2024-08-23 22:45:54

您正在“设置”该变量/属性的值。不覆盖或超载它。你的代码非常非常常见和正常。

所有这些术语(“设置”、“覆盖”、“过载”)都有特定的含义。 Override 和 Overload 与多态性(子类化)有关。

来自 http://en.wikipedia.org/wiki/Object-oriented_programming

多态性允许程序员像对待父类成员一样对待派生类成员。更准确地说,面向对象编程中的多态性是属于不同数据类型的对象响应同名方法的方法调用的能力,每个方法都根据适当的特定于类型的行为。一种方法或一个运算符(例如 +、- 或 *)可以抽象地应用于许多不同的情况。如果狗被命令说话(),这可能会引起吠声()。然而,如果命令 Pig 说话(),这可能会引发 oink()。它们都从Animal继承speak(),但是它们的派生类方法重写了父类的方法;这就是重写多态性。重载多态性是使用一种方法签名或一种运算符(例如“+”)来根据实现来执行多种不同的功能。例如,“+”运算符可用于执行整数加法、浮点加法、列表串联或字符串串联。 Number 的任何两个子类(例如 Integer 和 Double)都应该在 OOP 语言中正确相加。因此,该语言必须重载加法运算符“+”才能以这种方式工作。这有助于提高代码的可读性。其实现方式因语言而异,但大多数 OOP 语言至少支持某种程度的重载多态性。

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).

From 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.

貪欢 2024-08-23 22:45:54

如果您打算遵循给出的示例(使用 getter/setter 或在构造函数中设置它),请将其更改为 private,因为这些是控制变量中设置内容的方法。

将所有这些东西添加到类中而将属性保持为公共是没有意义的。

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.

It doesn't make sense to keep the property public with all those things added to the class.

北斗星光 2024-08-23 22:45:54

将 getter 和 setter 方法添加到您的类中。

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

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

Add getter and setter method to your class.

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

public function getValue()
{
    return $this->testvar;        
}
月光色 2024-08-23 22:45:54

class Testclass 内:

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

Inside class Testclass:

public function __construct($new_value)
{
    $this->testvar = $new_value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文