?? ??? ? ??
1, PHP ???? ??
???? ?????(??: ???? ?????, ??: OOP)?? ??? ??? ??? ?????. ???? ?? ??? ?? ??? ??????.
?? ???? ??? ???? ?? ???, ????, ??? ?? ?????.
??? ? ?? ?? ??:
?? ??: ??? ??? ? ?? ??, ? ??? ?? ?? ??? ?????. ??? ??: ?? ??? ???? ? ??? ???? ??, ??, ??, ??. ??? ??: ??? ??? ???? ????, ?? ??? ??? ??? ??? ?????.
?? ??, Animal? ?? ?? ??? ? ?? ?? ?????, ?? ?? ?? ??? ??, ? ? ??, ?? ? ?? ?? ?? ??? ????.
?? ?? ???
??? - ??? ???? ??? ?????. ???? ???? ???? ??? ???? ?? ??? ?????.
Object - ???? ???????.
?? ?? - ??? ??? ??? ?????. ? ??? ?? ?? ???? ??? ??? ?? ??? ?? ???? ? ????. ???? ??? ??????? ??? ??? ????? ?? ? ????.
?? ?? - ??? ??? ???? ??? ??? ???? ????? ? ??? ? ????.
?? - ??? ?? ???? ?? ???? ??? ??? ???? ???? ???? ???????. ???? ???? ??? ? ?? ???? ???? ? ? ??, ?? ????? ??? ???? ??? ???? ???? ??? ???? ??? ? ????.
?? ??? - ???? ?? ???? ?? ?????. ? ???? ?? ???, ?? ??? ?? ?? ????? ?? ? ????.
?? ??? - ?? ???? ???? ???? ?? ????? ???? ?? ?????? ???.
??? - ???? ??? ??? ???? ?? ??? ??? ???? ?? ??? ?? ? ??? ?????. ??? ???? ??? ? ?? ?? ??? ?? ?? ??? ??? ? ?? ??? ?????? ???.
???? - ??? ???, ??? ??? ???? ??? ?? ??? ???? ????? ?? ?? ????? ???.
??? - ???? ??? ??? ??(??)? ??(??)? ?? ??? ???? ????? ?? ?????. ???? ?? ?? ???? ????? ??????? ??? ??? ??? ???? ??????. ?? ???? ??? ?????? ?? ?? ????? ????? ???.
Encapsulation - ???? ?? ??? ???? ??? ??? ??? ??? ??? ??? ???? ?? ????.
Constructor - ??? ??? ? ??? ????? ? ?? ?????. ?, ?? ?? ??? ?? ?? ???? ? ?????. ??? ???? ?? ??? ?? new ???? ?? ?????.
Destructor - ???(???) ????? ?? ??? ?? ??? ???(?: ??? ??? ??? ??? ??) ???? ???? ???? ?????. ???? ?? "??" ??? ???? ? ?????(?? ?? ??? ??? ? new? ???? ??? ??? ???. ??? ??? ???? ?? ????? ??? ?? ????? ???).
2. ?? ??? ??
?? ?? ????? ???? ???? ? ?? ??? ?????. ???? ??? ???? ?? ???? ?????. ?, ???? ??? ??? ???. ??? ????? ??? ??? ??? ?????? ?? ?? ??? ????? ?? ??? ??, ??, ??, ?? ?? ??? ???. ??? ??? ??? ??? ????. ??? ???? ???? ???. ??? ?? ??? ??? ??? ? ??? ??? ? ??? ???? ????. ?? ????? ?? ????? ???. ??. ??? ?? ???????
?? Student.class.php ??? ??? ?? ???? ????. (???? ?? ???? ??? ???? ?? ????? ??? ??? ??? ?? ???? ?????. ".class.php"??, ??? ??? ?? ?? ??? ??? ???? ???.)
??: ???? ?? ??? ??? ??? ???? ?? ?????. php ???? ??? ??? ?? ??? ??? ? ????.
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 9:42 */ //定義student類 class Student{ //成員屬性 //成員方法 }
(1) ?? ??? ???? ??? ??? ?? ????? ?????. ??? ???? ????, ? ?? ??? ?? ????, ??? ? ??? ????, ??? ???? ???.
(2) ??? ??? ???? ??? ???? ???(URL, HTML? ?? ??? ? ?? ???? ?? ?).
(3) ??? ??? ???? ?? ?? ???? ???? ?????. ??? ???? ???? ??? ???? ???? ???? ??? ??? ? ??? ???? ???? ?? ????.
(4) ? ?? ???? ??? ??? ??? ???? ??? ? ??? ???? ?? ??? ??? ? ????.
?? ?? : : : class userAccount {
...
}
class PaintingOrder {
...
}
3, ?? ?? ? ?? ???? ??
<?php
class Student{
//成員屬性
public $studentId; //學(xué)生學(xué)號
public $studentName;//學(xué)生姓名
public $studentAge; //學(xué)生年齡
//成員方法
public function goSchool(){
echo "{$this->studentName}去上學(xué)<br>";
}
public function study($time){
echo "學(xué)習(xí)到{$time}";
}
}
4. ?? ??
? PHP ??? ???? ?? ??? ????????object.php:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 10:25 */ header("content-type:text/html;charset=utf8"); require './Student.class.php'; $student=new Student(); echo "<pre>"; var_dump($student);//查看對象的類型以及具體數(shù)據(jù) echo "</pre>";
?? ??? ??? ????.
5, ?? ?? ??
?? ??? ??? ??? ???? ??? ??? ?? null?? ? ? ????. ???? ??? ?????.
??? ??? ????.
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 10:25 */ header("content-type:text/html;charset=utf8"); require './Student.class.php'; $student=new Student(); $student->studentName='小張'; $student->studentId=1; $student->studentAge=25; echo "<pre>"; var_dump($student); echo "</pre>";
Print ??? ??? ????:
6, ?? ??? ??
??? ?? ?? ??:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 10:25 */ header("content-type:text/html;charset=utf8"); require './Student.class.php'; $student=new Student(); $student->studentName='小張'; $student->studentId=1; $student->studentAge=25; $student->goSchool(); $student->study("22:00");
?? ??: