【问题标题】:PHP simple function, need some explanationPHP简单函数,需要一些解释
【发布时间】:2015-06-21 13:59:30
【问题描述】:

我敢打赌这对你来说很简单,但它仍然让我感到困惑,我想在继续学习更高级的东西之前理解这一点。所以希望你能帮我说清楚。

代码如下:

class example
{
    public function NumberInput($number)
    {
        $this->number = $number;
    }

    public function NumberOutput()
    {
        return $this->number;
    }
}

$b = new example;
$b->NumberInput(7);
echo $b->NumberOutput();

所以代码可以正常工作,但我不明白这部分是如何工作的:

public function NumberInput($number)
{
    $this->number = $number;
}

如果我想输入一些数字,但我不想将它保存在另一个变量中,例如变量 $a。这不起作用(我不知道为什么):

public function NumberInput($number)
{
    $this->number = $a;
}

对此有何解释?

提前致谢。

【问题讨论】:

标签: php function class


【解决方案1】:

NumberInput 接受一个参数 ($number) 并将其分配给对象的一个​​属性。

在您的示例中, $b 是对象示例的一个实例。对象 $b(和类示例)有两个方法 NumberInput 和 NumberOutput,它们作用于属性 $number。

当您使用数字调用 NumberInput 时,它会将通过参数 $number 传入的值分配给属性 $number。

$this->number = $a 不起作用,因为 $a 没有定义,也没有作为参数传入。

【讨论】:

    【解决方案2】:
    class example{
    
        public function NumberInput($number){
            // this line takes the function parameter $number 
            // and creates a property of this object on the fly
            // ALSO called number ( $this->number )
            $this->number = $number;
        }
    
    
        public function NumberOutput(){
           // this returns the value in the property 
           // of this object called number
           return $this->number;
        }
    
      }
    
    $b = new example;
    $b->NumberInput(7);
    echo $b->NumberOutput();
    

    这样写会更好

    class example{
    
        // this is the number property defined 
        // rather than being created on the fly in NumberInput
        // defined as private as you have a GETTER function that
        // users of the class shoudl use to see its value so it sort of
        // implies it should not be available as a public property
        private $number;   
    
        public function NumberInput($number){
            // move parameter $number value to my property called number
            $this->number = $number;
        }
    
    
        public function NumberOutput(){
           // this returns the value in the property 
           // of this object called number
           return $this->number;
        }
    
      }
    
    $b = new example;
    $b->NumberInput(7);
    echo $b->NumberOutput();
    

    这不起作用

    public function NumberInput($number)
    {
        $this->number = $a;
    }
    

    因为$a在哪里定义,所以不存在

    你可以这样做

    public function NumberInput($a)
    {
        $this->number = $a;
    }
    

    或者

    public function NumberInput($number)
    {
        $this->a = $number;
    }
    

    【讨论】:

      【解决方案3】:

      我建议遵循一些标准的(几乎普遍使用的)面向对象的代码约定,这将使您的代码更具可读性,并且应该让您更清楚它是如何工作的:

      1. 类名大写,函数名不要大写
      2. 始终声明成员变量(通过 $this-> 访问)。 PHP 允许您在不声明它们的情况下使用它们,但声明它们会使代码更具可读性,并启用 IDE 等工具来帮助您完成代码等。
      3. 使用有意义的变量名,不要根据变量的类型来命名变量(这可以改变,在 PHP 的情况下可以混合使用)。所以,'number' 没有意义,'id'、'numChildren' 和 'age' 这样的名字是有意义的。
      4. 始终使用名为 setXXX 和 getXXX 的方法来访问成员变量。此类方法的标准术语是“访问器”。

      使用这些约定,这里有一个类似于你的示例类,它可以接受一个数值并将其保存在不同的成员变量中::

      class Example
      {
          private $id;
          private $age;   
          private $numChildren
      
          public function setId($number)
          {
              $this->id = $number;
          }
      
          public function getId()
          {
              return $this->id;
          }
      
          public function setAge($number)
          {
              $this->age = $number;
          }
      
          public function getAge()
          {
              return $this->age;
          }
      
          public function setNumChildren($number)
          {
              $this->numChildren = $number;
          }
      
          public function getNumChildren()
          {
              return $this->numChildren;
          }
      }
      

      注意 - 请注意,我实际上并不提倡使用 $number 作为 setter 方法的参数名称,但我保留它是为了说明如何将数字保存到类的不同成员变量中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-30
        • 1970-01-01
        • 2021-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-18
        相关资源
        最近更新 更多