how is age changed in object oriented tutorial

http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/
<?php
class Person{
  private $_name;
  private $_job;
  private $_age;
  
  public function __construct($name,$job,$age){
    $this->_name=$name;
    $this->_job=$job;
    $this->_age=$age;
  }
  public function changeJob($newjob)
  {
    $this->_job=$newjob;
  }
  public function happyBirthday()
  {
    ++$this->_age;
  }
}
//create two new people
$person1=new Person("Tom","Button-Pusher",34);
$person2=new Person("John","Lever Puller",41);

//output their starting point
echo "<pre>Person 1: ",print_r($person1,TRUE),"</pre>";
echo "<pre>Person 2: ",print_r($person2,TRUE),"</pre>";

//give tom a promotion and a birthday
$person1->changeJob("Box-Mover");
$person1->happyBirthday();

$person2->happyBirthday();

//output ending values
echo "<pre>Person 1: ",print_r($person1,TRUE),"</pre>";
echo "<pre>Person 2: ",print_r($person2,TRUE),"</pre>";

Open in new window



How does happyBirthday method know that it is changing age

  public function happyBirthday()
  {
    return 60;
  }

  public function happyBirthday()
  {
    return '60';
  }

does not change the age
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
In the original code snippet we find this:
public function happyBirthday()
  {
    ++$this->_age;
  }

Open in new window

I believe the intent of the code is to add one to the $this->_age value each time the happybirthday() method is called.

The method does not return any information, and that is probably why the script accessed it this way (without an assignment operator)

$person1->happyBirthday();
0
rgb192Author Commented:
public function happyBirthday()
  {
    ++$this->_age;
  }

or

public function happyBirthday()
  {
   return ++$this->_age;
  }

I do not understand because I think these are instance variables not static variables
so how can a variable change in the class and outside of the class
0
Ray PaseurCommented:
I agree with you, it might make more sense to return the age value.  Try running this to see the moving parts.

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

class Person
{
    private $_name;
    private $_job;
    private $_age;

    public function __construct($name,$job,$age)
    {
        $this->_name=$name;
        $this->_job=$job;
        $this->_age=$age;
    }

    public function changeJob($newjob)
    {
        $this->_job=$newjob;
    }

    public function happyBirthday()
    {
      return ++$this->_age;
    }
}
//create two new people
$person1 = new Person("Tom","Button-Pusher",34);
$person2 = new Person("John","Lever Puller",41);

//output their starting point
var_dump($person1);
var_dump($person2);

//give tom a promotion and a birthday
$person1->changeJob("Box-Mover");
var_dump($person1->happyBirthday());

// GIVE JOHN SOME BIRTHDAYS
$x = $person2->happyBirthday();
var_dump($x);
$x = $person2->happyBirthday();
$x = $person2->happyBirthday();
$x = $person2->happyBirthday();
var_dump($x);

$x = $person2->happyBirthday();
var_dump($x);

$x = $person2->happyBirthday();
var_dump($x);

$x = $person2->happyBirthday();
var_dump($x);

Open in new window

0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Chris StanyonWebDevCommented:
@rgb192 - not sure what you mean by changing inside the class and outside. You code has 3 instance variables (name, age and job), and 2 methods for setting the age and the job. It has no publicly accessible properties and no methods to retrieve the properties.

The happyBithday() function simply increases the instance variable $_age by 1 each time it's called.

Which bit are you struggling to understand?
0
Ray PaseurCommented:
It might also make sense to make the variables (properties) protected instead of private.  Private properties are not visible to extensions of the class.
http://php.net/manual/en/language.oop5.visibility.php

As a general rule, unless you can think of an object-oriented design reason to deviate, make your properties protected and your methods public.  In the long run, that will be a sensible design most of the time.
0
rgb192Author Commented:
I used the code example but I still do not understand how

$x = $person2->happyBirthday();

can change the age

maybe

$age= happyBirthday()
would be new age
0
Ray PaseurCommented:
Let's deconstruct this:

$x = $person2->happyBirthday();

That is saying the following in this order:

1. Locate the variable that is referred to by the $person2 name
2. If the variable does not point to an object, raise an error
3. Attempt to find the HappyBirthday() method in the class definition of the object
4. If the HappyBirthday() method does not exist in the class, raise an error
5. Execute the code in the HappyBirthday() method, passing no arguments
6. Assign the value that is returned from the method to the $x variable

Since we have more than one instance of the Person class, it follows that each person has his own birthday.  The birthday is stored as a "class property" meaning that it is a data element that is unique to the object instance of the Person class.
0
Chris StanyonWebDevCommented:
In your original code, happyBirthday() doesn't return a value so you can't assign it to a variable. This won't work:

$x = $person2->happyBirthday(); //There's nothing to assign to $x!

This code:

public function happyBirthday() {
   ++$this->_age;
}

Open in new window

means that calling $person->happyBirthday() simply adds 1 to the internal variable called $_age

If you want to assign the age to a variable, then you'll need to use Ray's example and return a value from the happyBirthday() function.

public function happyBirthday() {
   return ++$this->_age;
}

Open in new window

Now, when you call happyBirthday, not only will it add 1 to the internal $_age variable, but it will return that value as well, so you can use:

$x = $person2->happyBirthday(); //now the function returns a value so we can assign it.
0
rgb192Author Commented:
now I do not understand the difference between assignment

$x=$person2->happyBirthday();
and
$person2->happyBirthday();
0
Chris StanyonWebDevCommented:
OK. I think you need to go back to the basics of PHP!

When you call a function, that function will perform certain actions. Once it's done, it will either finish quietly or it will give you back a value (return!). Obviously, if it returns a value, you may want that value so you can use it later. Let's assume for a minute that your happyBirthday() function gives you back the age:

//happyBirthday() adds 1 to the current age and gives you back that value.
$x = $person2->happyBirthday();

//you now have that value in the $x variable to use as you like
echo $x; 
echo "Person 1 has an age of " . $x;

Open in new window

Now, if you don't need that value for anything else (or the function doesn't Return a value), then just call the function:

//we dont need the value so don't assign it, or the function doesn't return a value!
$person2->happyBirthday(); 

Open in new window

Make sense??
0
Ray PaseurCommented:
A function (or method, these words mean the same thing) that does not return anything is kind of an anti-design.  How would you know it did its job?  There really should be some kind of signal!  Required reading here:
http://php.net/manual/en/language.functions.php

Now with that said, "setter" and "getter" methods are complementary.  A setter might not return any value at all, even though it mutated the object by adding or changing a property.  The getter would be called to retrieve the value in the object's property.
0
rgb192Author Commented:

Now, if you don't need that value for anything else (or the function doesn't Return a value), then just call the function:
//we dont need the value so don't assign it, or the function doesn't return a value!
$person2->happyBirthday();

This seams like static variables to me
where static variables are edited in the class and the change is visible outside the class

A function (or method, these words mean the same thing) that does not return anything is kind of an anti-design.  How would you know it did its job?  There really should be some kind of signal!  Required reading here:
http://php.net/manual/en/language.functions.php

Now with that said, "setter" and "getter" methods are complementary.  A setter might not return any value at all, even though it mutated the object by adding or changing a property.  The getter would be called to retrieve the value in the object's property.

Is happybirthday() or the $_age a getter or setter
0
Ray PaseurCommented:
Technically, happyBirthday() is neither is a setter or getter.  It's a mutator.  The class constructor takes name, job and age.  As such it's both the constructor and a setter for name, job and age.  When you run happyBirthday() as shown in the original post, it adds one to the age, but it does not set or return the age, it just changes the age.  changeJob() is a good example of a setter.  It takes information from outside the object and injects it into the object.  There are no getters in the original code example.  And since the properties are private, you can look at them with print_r() but you cannot access them except by modifying the class definition.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
Now, if you don't need that value for anything else (or the function doesn't Return a value), then just call the function:

When you run happyBirthday() as shown in the original post, it adds one to the age, but it does not set or return the age, it just changes the age.


Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.