Home > php教程 > php手册 > body text

PHP面向对象的基本写法(区别于java)

WBOY
Release: 2016-06-06 19:49:22
Original
825 people have browsed it

?php/*** 这是一个人的对象*/class ClassName {public $name;public $age;public $sex; public static $my = 'static var11';function __construct($name,$age,$sex){ //这里已经是本身的属性了,可以看过$this已经有一个$了,声明了是地址,可以不同再次使

<?php /**
* 这是一个人的对象
*/
class ClassName 
{
	
	public $name;
	public $age;
	public $sex;<br />       public static $my = 'static var11';
	function __construct($name,$age,$sex)
	{
       //这里已经是本身的属性了,可以看过$this已经有一个$了,声明了是地址,可以不同再次使用地址
		$this->name = $name;
		$this->age = $age;
		$this->sex = $sex;
	}
	function say(){
		echo "Hello world";<br>               echo self::$my . "\n";<br>	}

}
$student = new ClassName("sun",20,"男");


echo $student->say();
echo "<br>";
echo $student->name;
echo "<br>";
echo $student->age;
echo "<br>";
echo $student->sex;
//调用的时候倒是和java语法差不多,就是“.”变成了“->”
?>
Copy after login

 构造方法的名字也进行了统一 __construct();

new 的时候同样是分配一个栈空间的地址指向堆空间的对象区域

调用构造方法parent::__construct() 和java中的super.funName()对比

self::用来访问本类的属性和方法 在类的内部对成员或方法进行访问的<br><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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template