Home > Backend Development > PHP Tutorial > PHP类定义与实例化

PHP类定义与实例化

WBOY
Release: 2016-06-23 13:27:24
Original
1077 people have browsed it

<?phpclass People{	public $name = 'jack';	public $height = 30;	public $age;	//不赋值默认NULL	static public $head = 4;	//静态属性,只有一个,所有对象共享,一改全改	const PI=3.14;	//类常量,仅类内可用	public function cry(){		echo '呱呱坠地';		echo self::$head;	//输出静态变量,用类::变量,自身类用self代替,同一个函数体内不能再有$this	 }	public function time(){		echo '现在的时间是',time();	//此处是系统的time函数 	}	public function __construct($age=0){		//构造函数,两个下划线		echo '哈哈,构造函数' . '<br />';		$this->age = $age;	//访问成员变量用$this	 }	public function __destruct(){		echo '<br />' . '呜呜,析构函数';	 }	//封装,保护变量不能随意更改,只能通过特定接口	private $money = 1000;	final public function showmen(){		//final修饰,能继承不能重写		return $this->money * 0.8;	 }	//普通方法,存放在类内,只有一份,必须用对象调用	//静态方法,存放在类内,只有一份,可以用类名调用	static public function scry(){		echo '呜呜';	 } }$a = new People();echo $a->name . '<br />';echo $a->age . '<br />';echo People::PI . '<br />';echo $a->time() . '<br />';echo People::cry() . '<br />';echo People::scry() . '<br />';
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template