-3

I bet this is pretty simple for you but it is still confusing me and I would like to understand this before I continue to learn more advanced stuff. So I hope you can make it clear for me.

The code is following:

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

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

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

So that code work fine, but I don't understand how exactly this part works:

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

What if I want for example input some number, but I wan't to save it in another variable, for example variable $a. This does not work ( and I don't know why):

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

Any explanation about this ?

Thanks in advance.

redbirdo
  • 4,937
  • 1
  • 30
  • 34
gilley
  • 49
  • 3
  • 5
  • 1
    Suggested reading: http://php.net/manual/en/language.oop5.basic.php – nicael Jun 21 '15 at 14:01
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Mike Szyndel Jun 21 '15 at 14:10

3 Answers3

0

NumberInput accepts a parameter ($number) and assigns it to a property of the object.

In your example, $b is an instance of the object example. Object $b (and class example) has two methods NumberInput and NumberOutput which act on the property $number.

When you call NumberInput, with a number, it assigns the property $number the value passed in through the parameter $number.

$this->number = $a doesn't work because $a is not defined and is not passed in as a parameter.

user2182349
  • 9,569
  • 3
  • 29
  • 41
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();

It would be better written like this

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

This does not work

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

because where is $a defined, it does not exist

You could however do

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

Or

public function NumberInput($number)
{
    $this->a = $number;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

I suggest following a few standard (pretty much universally used) object-oriented code conventions which will make your code more readable and should make it clearer to you how it works:

  1. Capitalise class names, do not capitalise function names
  2. Always declare member variables (accessed via $this->). PHP allows you to use them without declaring them but declaring them makes the code more readable and enables tools such as IDEs to help you out with code completion etc.
  3. Use meaningful variable names and don't name variables according to their type (which can change and in the case of PHP can be mixed). So, 'number' is not meaningful, names like 'id', 'numChildren' and 'age' are meaningful.
  4. Always use methods named setXXX and getXXX to access member variables. The standard term for such methods is 'accessors'.

Using these conventions, here is an example class similar to yours which can take a numeric value and save it in different member variables::

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

NB - note I wouldn't actually advocate using $number as the name of the argument to the setter methods but I've left it that way to illustrate how you can save a number into different member variables of a class.

redbirdo
  • 4,937
  • 1
  • 30
  • 34