PHP简单的功能,需要一些解释

时间:2015-06-21 13:59:31

标签: php function class

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

代码如下:

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;
}

对此有何解释?

提前致谢。

3 个答案:

答案 0 :(得分:0)

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

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

当您使用数字调用NumberInput时,它会通过参数$ number为传递的值指定属性$ number。

$ this-> number = $ a无效,因为$ a未定义且未作为参数传入。

答案 1 :(得分:0)

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;
}

答案 2 :(得分:0)

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

  1. 大写类名,不要大写函数名
  2. 始终声明成员变量(通过$ this->访问)。 PHP允许您在不声明它们的情况下使用它们,但声明它们使代码更具可读性,并使IDE等工具能够帮助您完成代码完成等。
  3. 使用有意义的变量名称,不要根据类型对变量进行命名(可以更改,并且在PHP的情况下可以混合使用)。所以,'数字'没有意义,像'id','numChildren'和'age'这样的名字是有意义的。
  4. 始终使用名为setXXX和getXXX的方法来访问成员变量。这些方法的标准术语是“访问者”。
  5. 使用这些约定,这里是一个类似于你的示例类,它可以取一个数值并将其保存在不同的成员变量中::

    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方法的参数名称,但我已经用它来说明如何将数字保存到类的不同成员变量中。 / p>

相关问题