在php中访问实例变量

时间:2017-07-12 04:53:58

标签: php

我已编写此代码我想增加$ age值,$ age被声明为类的静态变量如何访问$ age varible并增加其值我正在学习基础知识请帮助我guyz

<?php
class Person{
    public $age;
   public function __construct($initialAge){
          // Add some more code to run some checks on initialAge
       if($initialAge<0)
       {
           echo"Age is not valid, setting age to 0.\n";
           $age=0;

       }
       else{
           $age=$initialAge;

       }

    }
   public  function amIOld(){
            // Do some computations in here and print out the correct statement to the console 
       if($age<13){
           echo"You are young.\n"; echo $age;
       }
       else if($age>=13 && $age<18)
       {
            echo"You are a teenager.\n";  
       }
       else{
           echo"You are old.\n"; 

       }

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


}

1 个答案:

答案 0 :(得分:0)

请你试试这样的事情 $this->age+=1;

$this指向当前班级