<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 訪問控制(可見性) 對屬性或方法的訪問控制,是通過在前面添加關鍵字 public (公有),protected(受保護)或 private (私有)來實現的。被定義為公有的類成員可以在任何地方被訪問。被定義為受保護的類成員則可以被其自身以及其子類和父類訪問。被定義為私有的類成員則只能被其定義所以在的類訪問。 ### 屬性的訪問控制 類屬性必須定義為公有,受保護,私有之一。如果 var 定義,則被視為公有。 **Example #1 屬性聲明** ``` <?php /** ?*?Define?MyClass ?*/ class?MyClass { ????public?$public?=?'Public'; ????protected?$protected?=?'Protected'; ????private?$private?=?'Private'; ????function?printHello() ????{ ????????echo?$this->public; ????????echo?$this->protected; ????????echo?$this->private; ????} } $obj?=?new?MyClass(); echo?$obj->public;?//?這行能被正常執行 echo?$obj->protected;?//?這行會產生一個致命錯誤 echo?$obj->private;?//?這行也會產生一個致命錯誤 $obj->printHello();?//?輸出?Public、Protected?和?Private /** ?*?Define?MyClass2 ?*/ class?MyClass2?extends?MyClass { ????//?可以對?public?和?protected?進行重定義,但?private?而不能 ????protected?$protected?=?'Protected2'; ????function?printHello() ????{ ????????echo?$this->public; ????????echo?$this->protected; ????????echo?$this->private; ????} } $obj2?=?new?MyClass2(); echo?$obj2->public;?//?這行能被正常執行 echo?$obj2->private;?//?未定義?private echo?$obj2->protected;?//?這行會產生一個致命錯誤 $obj2->printHello();?//?輸出?Public、Protected2?和?Undefined ?> ``` > **Note**: 為了兼容性考慮,在 PHP 4 中使用 *var* 關鍵字對變量進行定義的方法在 PHP 5 中仍然有效(只是作為 public 關鍵字的一個別名)。在 PHP 5.1.3 之前的版本,該語法會產生一個 **`E_STRICT`** 警告。 ### 方法的訪問控制 類中的方法可以被定義為公有、私有或受保護。如果沒有設置這些關鍵字,則該方法默認為公有。 **Example #2 方法聲明** ``` <?php /** ?*?Define?MyClass ?*/ class?MyClass { ????//?聲明一個公有的構造函數 ????public?function?__construct()?{?} ????//?聲明一個公有的方法 ????public?function?MyPublic()?{?} ????//?聲明一個受保護的方法 ????protected?function?MyProtected()?{?} ????//?聲明一個私有的方法 ????private?function?MyPrivate()?{?} ????//?此方法為公有 ????function?Foo() ????{ ????????$this->MyPublic(); ????????$this->MyProtected(); ????????$this->MyPrivate(); ????} } $myclass?=?new?MyClass; $myclass->MyPublic();?//?這行能被正常執行 $myclass->MyProtected();?//?這行會產生一個致命錯誤 $myclass->MyPrivate();?//?這行會產生一個致命錯誤 $myclass->Foo();?//?公有,受保護,私有都可以執行 /** ?*?Define?MyClass2 ?*/ class?MyClass2?extends?MyClass { ????//?此方法為公有 ????function?Foo2() ????{ ????????$this->MyPublic(); ????????$this->MyProtected(); ????????$this->MyPrivate();?//?這行會產生一個致命錯誤 ????} } $myclass2?=?new?MyClass2; $myclass2->MyPublic();?//?這行能被正常執行 $myclass2->Foo2();?//?公有的和受保護的都可執行,但私有的不行 class?Bar? { ????public?function?test()?{ ????????$this->testPrivate(); ????????$this->testPublic(); ????} ????public?function?testPublic()?{ ????????echo?"Bar::testPublic\n"; ????} ???? ????private?function?testPrivate()?{ ????????echo?"Bar::testPrivate\n"; ????} } class?Foo?extends?Bar? { ????public?function?testPublic()?{ ????????echo?"Foo::testPublic\n"; ????} ???? ????private?function?testPrivate()?{ ????????echo?"Foo::testPrivate\n"; ????} } $myFoo?=?new?foo(); $myFoo->test();?//?Bar::testPrivate? ????????????????//?Foo::testPublic ?> ``` ### 其他對象的訪問控制 同一個類的對象即使不是同一個實例也可以互相訪問對方的私有與受保護成員。這是由于在這些對象的內部具體實現的細節都是已知的。 **Example #3 訪問同一個對象類型的私有成員** ``` <?php class?Test { ????private?$foo; ????public?function?__construct($foo) ????{ ????????$this->foo?=?$foo; ????} ????private?function?bar() ????{ ????????echo?'Accessed?the?private?method.'; ????} ????public?function?baz(Test?$other) ????{ ????????//?We?can?change?the?private?property: ????????$other->foo?=?'hello'; ????????var_dump($other->foo); ????????//?We?can?also?call?the?private?method: ????????$other->bar(); ????} } $test?=?new?Test('test'); $test->baz(new?Test('other')); ?> ``` 以上例程會輸出: ~~~ string(5) "hello" Accessed the private method. ~~~
                  <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>

                              哎呀哎呀视频在线观看