0

For some string inputs ($age) I have to output back "you are young" ,"you are teenager" and " You are old" depends of the age inputed.

The problem with it , is that it seems my code is wrong somehow , I'm at beginning with OOP in PHP , so If you can help me with some explanations , I will be thankfully

<form action="classvsinstance.php" method="get">
    <input type="text" name="age" placeholder="age">
    <input type="submit" value="submit">
</form>

and

class Person{
    public $age;
    public function __construct($initialAge){
      // Add some more code to run some checks on initialAge
      if($initialAge < 0){
          $age = 0;
          print('Age is not valid, setting age to 0.');
      } else {
          $initialAge = $age;
      }
    }
    public  function amIOld(){

        if($age < 13){
            print("You are young.");
        }elseif(($age>=13) && ($age < 18)){
            print("You are a teenager.");
        }else{
            print("You are old");

    }
    public  function yearPasses(){
      // Increment the age of the person in here
        $age++;
    }


}

$age = $_GET['age'];
echo "Your age is " . $age . ".<br><br>";

instantiating the class

$p = new Person($age);

calling the method

$ -> amIOld();
echo "\n";

All the outputs for every number I put in the form , are "You are young".

I don't know why , and what am I doing wrong here , cuz I've tried all the ways (except the good one) .

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Adrian Gheorghe
  • 647
  • 6
  • 13
  • 1
    Inside your class use :`$this->age` instead of `$age` ! – Ismail RBOUH Jul 11 '16 at 21:38
  • 1
    In each of your class methods you need to reference the class `$age` rather than the local instance, you do this with `$this->age` which is *not the same* as the `$age` in each of your methods. `$age` in `yearPasses` is always going to be 0 -> 1 because that value is forgotten as soon as that function/method finishes. Same with `amIOld()` , you are not referencing the *class* value `$this->age` – Martin Jul 11 '16 at 21:43
  • 1
    Since you're not reporting getting "undefined variable" errors, you're probably running with display_errors and error_reporting disabled. They should never be off on a devel/debug system. – Marc B Jul 11 '16 at 21:44
  • 1
    Functions/methods never should be responsible for outputting things, return the value back to the caller and output from there – DarkBee Jul 11 '16 at 21:45
  • 1
    @DarkBee absolutely true but without a full answer with an example I think throwing keywords and concise concepts at the OP will simply confuse them. :-/ *(a general note not specifically at your comment)* – Martin Jul 11 '16 at 21:48
  • `$initialAge = $age;` is back to front! – Ken Y-N Jul 12 '16 at 01:36

1 Answers1

3

Your construct is assiging things backwards, it should be like this:

public function __construct($initialAge){
      // Add some more code to run some checks on initialAge
      if($initialAge < 0){
          $this->age = 0;
          print('Age is not valid, setting age to 0.');
      } else {
          $this->age = $initialAge;
      }
}

You want to be assigning from $initialAge to the class variable $age, not the other way around. Then, you need to refer to the class variable with the $this keyword:

public  function amIOld(){
        if($this->age < 13){
            print("You are young.");
        }elseif(($this->age>=13) && ($this->age < 18)){
            print("You are a teenager.");
        }else{
            print("You are old");
        }
     // ^ closing brace was also missing!
}

public  function yearPasses(){
      // Increment the age of the person in here
        $this->age++;
}

Since you're working with an object, you should be using other OOP features like throwing an exception if the age is not valid. And you should definitely not be outputting messages from inside your class. Let the code outside handle that, based on the return value from the class.

miken32
  • 42,008
  • 16
  • 111
  • 154