php面向对象

1.类的声明

[修饰类的关键字] class 类名 {

  成员属性;

  成员方法;

}

一个文件只保存一个类,文件名中包含类名,文件:类名.class.php

命名的写法:

变量:aaaBbbCcc

函数:aaaBbbCcc

常量:AAABBBCCC

类名:AaaBbbCcc

php面向对象
 1 class person {
 2     private $name;
 3     private $age;
 4     private $sex;
 5     private $score;
 6 
 7     function __construct($name,$age,$sex,$score){
 8         $this->name=$name;
 9         $this->age=$age;
10         $this->sex=$sex;
11         $this->score=$score;    
12     }
13     function say(){
14         echo ‘MY name is ‘.$this->name.‘<br />‘;
15         echo ‘I\‘am ‘.$this->age.‘ years old <br />‘;
16         echo ‘i am a ‘.$this->sex.‘<br />‘;
17         echo ‘My score is ‘.$this->score.‘<br />‘;
18         echo ‘<br/>‘;
19     }
20 }
21 $Jim=new person("Jim","18","boy","90");
22 $Lily=new person("Lily","17","girl","100");
23 
24 $Jim->say();
25 $Lily->say();
26 
27 
28 //result
29 MY name is Jim
30 I‘am 18 years old 
31 i am a boy
32 My score is 90
33 
34 MY name is Lily
35 I‘am 17 years old 
36 i am a girl
37 My score is 100
php面向对象

php面向对象,布布扣,bubuko.com

php面向对象

上一篇:Linux命令——chown命令


下一篇:linux下的nginx日志自动备份压缩--日志切割机