<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 一、概述 php通過class關鍵字聲明類,其中一個類包含屬性和方法兩部分;某類事物的實例,比如人類指類,那單個人就是對象。 ``` class Person { # 屬性 private $name = "cercy"; public $sex = "girl"; protected $height = 1.88; # 方法 public function setName($name){ $this->name = $name; } public function getName(){ return $this->name; } } # 實例化一個對象 $myObject = new Person(); echo $myObject->name; echo $myObject->getName(); ``` ## 二、構造函數 類中存在默認的構造函數,也可以指定構造函數,用于在實例化對象時指定屬性而不是使用默認的屬性,構造函數自動執行,無需手動調用。 ``` class Person { private $name = "jimson"; public $sex = "boy"; public function getName(){ return $this->$name; } public function __construct($name,$sex){ $this->$name = $name; $this->$sex = $sex; } } $newObj = new Person("Tim","girl"); ``` ## 三、析構函數 也是自動執行的函數,用于實例化對象之后將類銷毀。 ``` class Person { # …… public function __destruct(){ echo __CLASS__."被銷毀了"; } } $newObj = new Person("Tim","girl"); ``` ## 四、繼承 可以通過關鍵字extends來定義類,被繼承的類叫父類,以此繼承父類的非private屬性和方法。 ``` class Person { private $name = "jimson"; public $sex = "boy"; public function getName(){ return $this->$name; } public function __construct($name,$sex){ $this->$name = $name; $this->$sex = $sex; } } class Teacher extends Person{ public $salary = "3000"; public function teachStudent(){ echo "傳授學生知識"; } } $teacher = new Teacher("james","boy"); # 調用父類的方法 $teacher->getName(); # 調用自己的方法 $teacher->teachStudent(); ``` 子類也可以用自己的構造函數,同時調用父類的構造函數。 ``` class Person { private $name = "jimson"; public $sex = "boy"; public function getName(){ return $name; } public function __construct($name,$sex){ $this->$name = $name; $this->$sex = $sex; } } class Teacher extends Person{ public $salary = "3000"; public function getSalary(){ return $this->$salary; } public function __construct($name,$sex,$salary){ # 調用父類的構造函數 parent::__construct($name,$sex); $this->$salary = $salary; } } $teacher = new Teacher("james","boy","10000"); echo $teacher->getName(); //james echo $teacher->getSalary(); //10000 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看