php函数定义+关键字+继承小总结


```php
 money += $money;
		//和java中的this.name = name;
	}
}

?>
来访问对象的成员属性和成员方法 使用::来访问对象的成员常量和成员常量
// include 'Persion.class.php';
 $jack = new Persion ();
 $jack -> income(10000);
 echo "

jack的存款有:".$jack ->money; // echo "

".SCIENTIFIC_NAME; // echo "

". $jack::SCIENTIFIC_NAME_NAME; ?>


=========================================================
 gender = $gender;
		$this -> age = $age;
		$this -> name = $name;
		
	}
//	//构造方法的写法二
//	function Persion($gender,$age,$age){
//		$this -> gender = $gender;
//		$this -> age = $age;
//		$this -> name = $name;
//		/*
//		 * Fatal error: Redefinition of parameter $age in F:\xampp\htdocs\php\hanshu\day02.php on line 21
//		 * 在这里它报了一个错就是age重复定义了
//		 * */
//	}
/*
 * object(Student)#1 (3) {
  ["gender"]=>
  string(3) "man"
  ["age"]=>
  int(18)
  ["name"]=>
  string(8) "huangtao"
} 
 * 这是代码的输出结果
 * */
	
}

?>
";//
就是让输出的格式好看一点的东西	
 var_dump($huangtao);
?>
================================id = $id;
		this ->name = $name;
		this -> gender = $gender;
		this ->age = $age;
		this ->salary = $salary;
		this -> address = $address;
	}
	//使用析构函数清理内存
	function __destruct(){
		echo "运行结束,清理内存";
	}
	
}
	  
?>
";
   var_dump($huangtao);
   	
?>
========================================================


========================================================
 name = "jack";//相当于是给他赋予了一个值在此处使用到了伪函数
	   //该函数指向的是对象自己
	}
	  	
	
  }


?>
";
 echo $jack -> name;
// echo $jack -> id;
     /*
	  * -----> Fatal error: Uncaught Error: Cannot access private property Persion::$id in 
	  * F:\xampp\htdocs\php\hanshu\day05.php:38 Stack trace: #0 {main} thrown 
	  * in F:\xampp\htdocs\php\hanshu\day05.php on line 38
	  * 我们可以看到报了该错误就是指该id变量是私有的不可以访问
	  * */	
	  
	  
?>
=========================================================
 name = $name;
//		this -> id = $id ;
//		this -> salary = $salary;
//		
//	 }
//创建构造函数
 function __construct(){
 	$this -> name = "huangtao";
	$this -> id = 123;
	$this ->salary = 10000;
 }
 
}
?>
";
   echo $huangtao -> name;
   $id = new Persion();
   echo "
"; echo $id -> id; echo "
"; $salary = new Persion(); echo $salary->salary; /* * Fatal error: Uncaught Error: Cannot access private property Persion::$id in * F:\xampp\htdocs\php\hanshu\day06.php:31 Stack trace: #0 {main} thrown in * F:\xampp\htdocs\php\hanshu\day06.php on line 31 * */ //我们可以看到它报了以上的错误 ?> ========================================================= name = "Jack"; $this ->money = 10000; } } class Man extends Persion { //这里定义的是一个函数但是它是在前面加了修饰的函数就是加了一个公共的修饰 public public function numOfMoney(){ return $this -> money; } } $Jack = new Man(); echo $Jack ->numOfMoney(); $Jack = new Persion(); echo $Jack->money; /* * 10000 Fatal error: Uncaught Error: Cannot access protected property Persion::$money in * F:\xampp\htdocs\php\hanshu\day07.php:20 Stack trace: #0 {main} thrown in * F:\xampp\htdocs\php\hanshu\day07.php on line 20 * 我们可以看到它的返回值只有一个就是1000也就是说它修饰的方法只有他的子类能调用 * */ ?> ========================================================= name = $name; $this ->birthday = $birthday; } } //再创建一个Persion的子类来继承他 class Man extends Persion{ function __construct($name,$birthday){ parent:: __construct ($name,$birthday); $this ->gender = $this::MAN; } } class Woman extends Persion{ function __construct($name,$birthday){ parent:: __construct ($name,$birthday); $this ->gender = $this::WOMAN; } } $jack = new Man ("jack","19910101"); $jack = new Woman ("Lucy","19980501"); echo "
";
var_dump($jack);
var_dump($Lucy);
?>

你可能感兴趣的:(php函数定义+关键字+继承小总结)