简体   繁体   English

在php中访问实例变量

[英]Accessing instance variable in php

I have written this code I want to increment $age value, $age is declared as static variable of class how to access $age varible and increment its value i am learning the basics please help me guyz 我已经编写了这段代码,我想增加$ age的值,$ age被声明为类的静态变量,如何访问$ age变量并增加其值我正在学习基础知识,请帮助我

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


}

Can you please try bellow something like this 你能试试下面这样吗
$this->age+=1;

$this is point to current class $this是指向当前类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM