{"id":219,"date":"2022-08-03T09:20:45","date_gmt":"2022-08-03T01:20:45","guid":{"rendered":"http:\/\/162.14.82.114\/?p=219"},"modified":"2022-08-03T09:20:45","modified_gmt":"2022-08-03T01:20:45","slug":"php%e5%ad%a6%e4%b9%a0%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"http:\/\/162.14.82.114\/index.php\/219\/08\/03\/2022\/","title":{"rendered":"php\u5b66\u4e60\uff08\u4e8c\uff09"},"content":{"rendered":"<h2>PHP\u547d\u540d\u7a7a\u95f4<\/h2>\n<p>namespace<\/p>\n<p>PHP \u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u89e3\u51b3\u4ee5\u4e0b\u4e24\u7c7b\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u7f16\u5199\u7684\u4ee3\u7801\u4e0ePHP\u5185\u90e8\u7684\u7c7b\/\u51fd\u6570\/\u5e38\u91cf\u6216\u7b2c\u4e09\u65b9\u7c7b\/\u51fd\u6570\/\u5e38\u91cf\u4e4b\u95f4\u7684\u540d\u5b57\u51b2\u7a81\u3002<\/li>\n<li>\u4e3a\u5f88\u957f\u7684\u6807\u8bc6\u7b26\u540d\u79f0(\u901a\u5e38\u662f\u4e3a\u4e86\u7f13\u89e3\u7b2c\u4e00\u7c7b\u95ee\u9898\u800c\u5b9a\u4e49\u7684)\u521b\u5efa\u4e00\u4e2a\u522b\u540d\uff08\u6216\u7b80\u77ed\uff09\u7684\u540d\u79f0\uff0c\u63d0\u9ad8\u6e90\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ol>\n<h3>\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u5e38\u91cf\u3001\u7c7b\u548c\u51fd\u6570\u540d\u90fd\u653e\u5728\u5168\u5c40\u7a7a\u95f4\u4e0b\uff0c\u5c31\u548cPHP\u652f\u6301\u547d\u540d\u7a7a\u95f4\u4e4b\u524d\u4e00\u6837\u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\n    namespace MYSPACE;\n    \/\/\u4ee3\u7801\n?&gt;<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u9879\u76ee\u4e2d\u4f7f\u7528\u591a\u4e2a\u547d\u540d\u7a7a\u95f4\uff1a<\/p>\n<pre><code class=\"language-php\">&lt;?php\n namespace MYSPACE1;\n const PI = 3.14;\n class Circle{}\n function area(){}\n\n namespace MYSPACE2;\n const PI = 3.1415;\n class Circle{}\n function perimeter(){}\n?&gt;<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u65b9\u6cd5\u867d\u7136\u53ef\u4ee5\u4f46\u662f\u5e76\u4e0d\u88ab\u5efa\u8bae\uff0c\u5efa\u8bae\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-php\">&lt;?php\nnamespace MYSPACE1{\n    const PI = 3.14;\n    class Circle{}\n    function area(){}\n}\n\nnamespace MYSPACE2{\n    const PI = 3.1415;\n    class Circle{}\n    function perimeter(){}\n}\n?&gt;<\/code><\/pre>\n<p>\u5c06\u5168\u5c40\u7684\u975e\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u4ee3\u7801\u4e0e\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u4ee3\u7801\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u53ea\u80fd\u4f7f\u7528\u5927\u62ec\u53f7\u5f62\u5f0f\u7684\u8bed\u6cd5\u3002\u5168\u5c40\u4ee3\u7801\u5fc5\u987b\u7528\u4e00\u4e2a\u4e0d\u5e26\u540d\u79f0\u7684 namespace \u8bed\u53e5\u52a0\u4e0a\u5927\u62ec\u53f7\u62ec\u8d77\u6765<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\/\/declare(encoding=&#039;UTF-8&#039;);\n\/\/\u5728\u58f0\u660e\u547d\u540d\u7a7a\u95f4\u4e4b\u524d\u552f\u4e00\u5408\u6cd5\u7684\u4ee3\u7801\u662f\u7528\u4e8e\u5b9a\u4e49\u6e90\u6587\u4ef6\u7f16\u7801\u65b9\u5f0f\u7684 declare \u8bed\u53e5\u3002\u6240\u6709\u975e PHP \u4ee3\u7801\u5305\u62ec\u7a7a\u767d\u7b26\u90fd\u4e0d\u80fd\u51fa\u73b0\u5728\u547d\u540d\u7a7a\u95f4\u7684\u58f0\u660e\u4e4b\u524d\u3002\nnamespace { \/\/ \u5168\u5c40\u4ee3\u7801\nsession_start();\n$a = MyProject\\connect();\necho MyProject\\Connection::start();\n}\n?&gt;<\/code><\/pre>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u4f1a\u51fa\u73b0\u8bed\u6cd5\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-php\">&lt;html&gt;\n&lt;?php\nnamespace MyProject; \/\/ \u547d\u540d\u7a7a\u95f4\u524d\u51fa\u73b0\u4e86\u201c&lt;html&gt;\u201d \u4f1a\u81f4\u547d\u9519\u8bef -\u3000\u547d\u540d\u7a7a\u95f4\u5fc5\u987b\u662f\u7a0b\u5e8f\u811a\u672c\u7684\u7b2c\u4e00\u6761\u8bed\u53e5\n?&gt;<\/code><\/pre>\n<h3>\u5b50\u547d\u540d\u7a7a\u95f4<\/h3>\n<pre><code class=\"language-php\">&lt;?php\nnamespace MyProject\\Sub\\Level;  \/\/\u58f0\u660e\u5206\u5c42\u6b21\u7684\u5355\u4e2a\u547d\u540d\u7a7a\u95f4\nconst CONNECT_OK = 1;\nclass Connection { \/* ... *\/ }\nfunction Connect() { \/* ... *\/  }\n?&gt;<\/code><\/pre>\n<h3>\u547d\u540d\u7a7a\u95f4\u4f7f\u7528<\/h3>\n<ol>\n<li><strong>\u975e\u9650\u5b9a\u540d\u79f0\uff0c\u6216\u4e0d\u5305\u542b\u524d\u7f00\u7684\u7c7b\u540d\u79f0\uff0c<\/strong>\u4f8b\u5982 $a=new foo(); \u6216 foo::staticmethod();\u3002\u5982\u679c\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u662f currentnamespace\uff0cfoo \u5c06\u88ab\u89e3\u6790\u4e3a currentnamespace\\foo\u3002\u5982\u679c\u4f7f\u7528 foo \u7684\u4ee3\u7801\u662f\u5168\u5c40\u7684\uff0c\u4e0d\u5305\u542b\u5728\u4efb\u4f55\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u4ee3\u7801\uff0c\u5219 foo \u4f1a\u88ab\u89e3\u6790\u4e3afoo\u3002 \u8b66\u544a\uff1a\u5982\u679c\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u51fd\u6570\u6216\u5e38\u91cf\u672a\u5b9a\u4e49\uff0c\u5219\u8be5\u975e\u9650\u5b9a\u7684\u51fd\u6570\u540d\u79f0\u6216\u5e38\u91cf\u540d\u79f0\u4f1a\u88ab\u89e3\u6790\u4e3a\u5168\u5c40\u51fd\u6570\u540d\u79f0\u6216\u5e38\u91cf\u540d\u79f0\u3002<\/li>\n<li><strong>\u9650\u5b9a\u540d\u79f0,\u6216\u5305\u542b\u524d\u7f00\u7684\u540d\u79f0\uff0c<\/strong>\u4f8b\u5982 $a = new subnamespace\\foo(); \u6216 subnamespace\\foo::staticmethod();\u3002\u5982\u679c\u5f53\u524d\u7684\u547d\u540d\u7a7a\u95f4\u662f currentnamespace\uff0c\u5219 foo \u4f1a\u88ab\u89e3\u6790\u4e3a currentnamespace\\subnamespace\\foo\u3002\u5982\u679c\u4f7f\u7528 foo \u7684\u4ee3\u7801\u662f\u5168\u5c40\u7684\uff0c\u4e0d\u5305\u542b\u5728\u4efb\u4f55\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u4ee3\u7801\uff0cfoo \u4f1a\u88ab\u89e3\u6790\u4e3asubnamespace\\foo\u3002<\/li>\n<li><strong>\u5b8c\u5168\u9650\u5b9a\u540d\u79f0\uff0c\u6216\u5305\u542b\u4e86\u5168\u5c40\u524d\u7f00\u64cd\u4f5c\u7b26\u7684\u540d\u79f0\uff0c<\/strong>\u4f8b\u5982\uff0c $a = new \\currentnamespace\\foo(); \u6216 \\currentnamespace\\foo::staticmethod();\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cfoo \u603b\u662f\u88ab\u89e3\u6790\u4e3a\u4ee3\u7801\u4e2d\u7684\u6587\u5b57\u540d(literal name)currentnamespace\\foo\u3002<\/li>\n<\/ol>\n<h3>\u547d\u540d\u7a7a\u95f4\u548c\u52a8\u6001\u8bed\u8a00\u7279\u5f81<\/h3>\n<p>PHP \u547d\u540d\u7a7a\u95f4\u7684\u5b9e\u73b0\u53d7\u5230\u5176\u8bed\u8a00\u81ea\u8eab\u7684\u52a8\u6001\u7279\u5f81\u7684\u5f71\u54cd\u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\n    class classname\n{\n    function __construct()\n    {\n        echo __METHOD__,&quot;\\n&quot;;\n    }\n}\nfunction funcname()\n{\n    echo __FUNCTION__,&quot;\\n&quot;;\n}\nconst constname = &quot;global&quot;;\n$a = &#039;classname&#039;;\n$obj = new $a; \/\/ prints classname::__construct\n$b = &#039;funcname&#039;;\n$b(); \/\/ prints funcname\necho constant(&#039;constname&#039;), &quot;\\n&quot;; \/\/ prints global\n?&gt;<\/code><\/pre>\n<h3>namespace\u5173\u952e\u5b57\u548c<strong>NAMESPACE<\/strong>\u5e38\u91cf<\/h3>\n<pre><code class=\"language-php\">&lt;?php\nnamespace MyProject;\necho &#039;&quot;&#039;, __NAMESPACE__, &#039;&quot;&#039;; \/\/ \u8f93\u51fa &quot;MyProject&quot;\n?&gt;<\/code><\/pre>\n<h3>\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a\u522b\u540d\/\u5bfc\u5165<\/h3>\n<p>PHP \u547d\u540d\u7a7a\u95f4\u652f\u6301 \u6709\u4e24\u79cd\u4f7f\u7528\u522b\u540d\u6216\u5bfc\u5165\u65b9\u5f0f\uff1a\u4e3a\u7c7b\u540d\u79f0\u4f7f\u7528\u522b\u540d\uff0c\u6216\u4e3a\u547d\u540d\u7a7a\u95f4\u540d\u79f0\u4f7f\u7528\u522b\u540d\u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\nuse My\\Full\\Classname as Another, My\\Full\\NSname;\n$obj = new Another; \/\/ \u5b9e\u4f8b\u5316 My\\Full\\Classname \u5bf9\u8c61\nNSname\\subns\\func(); \/\/ \u8c03\u7528\u51fd\u6570 My\\Full\\NSname\\subns\\func\n?&gt;\n&lt;?php\nuse My\\Full\\Classname as Another, My\\Full\\NSname;\n$obj = new Another; \/\/ \u5b9e\u4f8b\u5316 My\\Full\\Classname \u7c7b\n$obj = new \\Another; \/\/ \u5b9e\u4f8b\u5316 Another \u7c7b\n$obj = new Another\\thing; \/\/ \u5b9e\u4f8b\u5316 My\\Full\\Classname\\thing \u7c7b\n$obj = new \\Another\\thing; \/\/ \u5b9e\u4f8b\u5316 Another\\thing \u7c7b\n?&gt;\/\/\u5bfc\u5165\u548c\u5b8c\u5168\u9650\u5b9a\u540d\u79f0\n&lt;?php\nuse My\\Full\\Classname as Another, My\\Full\\NSname;\n$obj = new Another; \/\/ \u5b9e\u4f8b\u5316\u4e00\u4e2a My\\Full\\Classname \u5bf9\u8c61\n$a = &#039;Another&#039;;\n$obj = new $a;      \/\/ \u5b9e\u9645\u5316\u4e00\u4e2a Another \u5bf9\u8c61\n?&gt;\/\/\u5bfc\u5165\u548c\u52a8\u6001\u540d\u79f0<\/code><\/pre>\n<h3>\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\uff1a\u540e\u5907\u5168\u5c40\u51fd\u6570\/\u5e38\u91cf<\/h3>\n<p>\u5728\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u5f53 PHP \u9047\u5230\u4e00\u4e2a\u975e\u9650\u5b9a\u7684\u7c7b\u3001\u51fd\u6570\u6216\u5e38\u91cf\u540d\u79f0\u65f6\uff0c\u5b83\u4f7f\u7528\u4e0d\u540c\u7684\u4f18\u5148\u7b56\u7565\u6765\u89e3\u6790\u8be5\u540d\u79f0\u3002\u7c7b\u540d\u79f0\u603b\u662f\u89e3\u6790\u5230\u5f53\u524d\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u540d\u79f0\u3002\u56e0\u6b64\u5728\u8bbf\u95ee\u7cfb\u7edf\u5185\u90e8\u6216\u4e0d\u5305\u542b\u5728\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7c7b\u540d\u79f0\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u5b8c\u5168\u9650\u5b9a\u540d\u79f0<\/p>\n<pre><code class=\"language-php\">&lt;?php\nnamespace A\\B\\C;\nclass Exception extends \\Exception {}\n$a = new Exception(&#039;hi&#039;); \/\/ $a \u662f\u7c7b A\\B\\C\\Exception \u7684\u4e00\u4e2a\u5bf9\u8c61\n$b = new \\Exception(&#039;hi&#039;); \/\/ $b \u662f\u7c7b Exception \u7684\u4e00\u4e2a\u5bf9\u8c61\n$c = new ArrayObject; \/\/ \u81f4\u547d\u9519\u8bef, \u627e\u4e0d\u5230 A\\B\\C\\ArrayObject \u7c7b\n?&gt;<\/code><\/pre>\n<h3>\u5168\u5c40\u7a7a\u95f4<\/h3>\n<pre><code class=\"language-php\">&lt;?php\nnamespace A\\B\\C;\n\/* \u8fd9\u4e2a\u51fd\u6570\u662f A\\B\\C\\fopen *\/\nfunction fopen() { \n     \/* ... *\/\n     $f = \\fopen(...); \/\/ \u8c03\u7528\u5168\u5c40\u7684fopen\u51fd\u6570\n     return $f;\n} \n?&gt;<\/code><\/pre>\n<h2>php\u9762\u5411\u7684\u5bf9\u8c61<\/h2>\n<pre><code class=\"language-php\">class Child extends Parent {\n   \/\/ \u4ee3\u7801\u90e8\u5206\n}\/\/\u7ee7\u627f<\/code><\/pre>\n<h3>\u8bbf\u95ee\u63a7\u5236<\/h3>\n<p>PHP \u5bf9\u5c5e\u6027\u6216\u65b9\u6cd5\u7684\u8bbf\u95ee\u63a7\u5236\uff0c\u662f\u901a\u8fc7\u5728\u524d\u9762\u6dfb\u52a0\u5173\u952e\u5b57 public\uff08\u516c\u6709\uff09\uff0cprotected\uff08\u53d7\u4fdd\u62a4\uff09\u6216 private\uff08\u79c1\u6709\uff09\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<ul>\n<li><strong>public\uff08\u516c\u6709\uff09\uff1a<\/strong>\u516c\u6709\u7684\u7c7b\u6210\u5458\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u88ab\u8bbf\u95ee\u3002<\/li>\n<li><strong>protected\uff08\u53d7\u4fdd\u62a4\uff09\uff1a<\/strong>\u53d7\u4fdd\u62a4\u7684\u7c7b\u6210\u5458\u5219\u53ef\u4ee5\u88ab\u5176\u81ea\u8eab\u4ee5\u53ca\u5176\u5b50\u7c7b\u548c\u7236\u7c7b\u8bbf\u95ee\u3002<\/li>\n<li><strong>private\uff08\u79c1\u6709\uff09\uff1a<\/strong>\u79c1\u6709\u7684\u7c7b\u6210\u5458\u5219\u53ea\u80fd\u88ab\u5176\u5b9a\u4e49\u6240\u5728\u7684\u7c7b\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n<h4>\u5c5e\u6027\u7684\u8bbf\u95ee\u63a7\u5236<\/h4>\n<p>\u7c7b\u5c5e\u6027\u5fc5\u987b\u5b9a\u4e49\u4e3a\u516c\u6709\uff0c\u53d7\u4fdd\u62a4\uff0c\u79c1\u6709\u4e4b\u4e00\u3002\u5982\u679c\u7528 var \u5b9a\u4e49\uff0c\u5219\u88ab\u89c6\u4e3a\u516c\u6709\u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\nclass MyClass\n{\n    public $public = &#039;Public&#039;;\n    protected $protected = &#039;Protected&#039;;\n    private $private = &#039;Private&#039;;\n\n    function printHello()\n    {\n        echo $this-&gt;public;\n        echo $this-&gt;protected;\n        echo $this-&gt;private;\n    }\n}\n\n$obj = new MyClass();\necho $obj-&gt;public; \/\/ \u8fd9\u884c\u80fd\u88ab\u6b63\u5e38\u6267\u884c\necho $obj-&gt;protected; \/\/ \u8fd9\u884c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u81f4\u547d\u9519\u8bef\necho $obj-&gt;private; \/\/ \u8fd9\u884c\u4e5f\u4f1a\u4ea7\u751f\u4e00\u4e2a\u81f4\u547d\u9519\u8bef\n$obj-&gt;printHello(); \/\/ \u8f93\u51fa Public\u3001Protected \u548c Private\n\nclass MyClass2 extends MyClass\n{\n    \/\/ \u53ef\u4ee5\u5bf9 public \u548c protected \u8fdb\u884c\u91cd\u5b9a\u4e49\uff0c\u4f46 private \u800c\u4e0d\u80fd\n    protected $protected = &#039;Protected2&#039;;\n\n    function printHello()\n    {\n        echo $this-&gt;public;\n        echo $this-&gt;protected;\n        echo $this-&gt;private;\n    }\n}\n\n$obj2 = new MyClass2();\necho $obj2-&gt;public; \/\/ \u8fd9\u884c\u80fd\u88ab\u6b63\u5e38\u6267\u884c\necho $obj2-&gt;private; \/\/ \u672a\u5b9a\u4e49 private\necho $obj2-&gt;protected; \/\/ \u8fd9\u884c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u81f4\u547d\u9519\u8bef\n$obj2-&gt;printHello(); \/\/ \u8f93\u51fa Public\u3001Protected2 \u548c Undefined\n\n?&gt;<\/code><\/pre>\n<h4>\u65b9\u6cd5\u7684\u8bbf\u95ee\u63a7\u5236<\/h4>\n<p>\u7c7b\u4e2d\u7684\u65b9\u6cd5\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a\u516c\u6709\uff0c\u79c1\u6709\u6216\u53d7\u4fdd\u62a4\u3002\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u8fd9\u4e9b\u5173\u952e\u5b57\uff0c\u5219\u8be5\u65b9\u6cd5\u9ed8\u8ba4\u4e3a\u516c\u6709\u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\nclass MyClass\n{\n    \/\/ \u58f0\u660e\u4e00\u4e2a\u516c\u6709\u7684\u6784\u9020\u51fd\u6570\n    public function __construct() { }\n    \/\/ \u58f0\u660e\u4e00\u4e2a\u516c\u6709\u7684\u65b9\u6cd5\n    public function MyPublic() { }\n    \/\/ \u58f0\u660e\u4e00\u4e2a\u53d7\u4fdd\u62a4\u7684\u65b9\u6cd5\n    protected function MyProtected() { }\n    \/\/ \u58f0\u660e\u4e00\u4e2a\u79c1\u6709\u7684\u65b9\u6cd5\n    private function MyPrivate() { }\n    \/\/ \u6b64\u65b9\u6cd5\u4e3a\u516c\u6709\n    function Foo()\n    {\n        $this-&gt;MyPublic();\n        $this-&gt;MyProtected();\n        $this-&gt;MyPrivate();\n    }\n}\n$myclass = new MyClass;\n$myclass-&gt;MyPublic(); \/\/ \u8fd9\u884c\u80fd\u88ab\u6b63\u5e38\u6267\u884c\n$myclass-&gt;MyProtected(); \/\/ \u8fd9\u884c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u81f4\u547d\u9519\u8bef\n$myclass-&gt;MyPrivate(); \/\/ \u8fd9\u884c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u81f4\u547d\u9519\u8bef\n$myclass-&gt;Foo(); \/\/ \u516c\u6709\uff0c\u53d7\u4fdd\u62a4\uff0c\u79c1\u6709\u90fd\u53ef\u4ee5\u6267\u884c<\/code><\/pre>\n<h3>\u63a5\u53e3<\/h3>\n<p>\u4f7f\u7528\u63a5\u53e3\uff08interface\uff09\uff0c\u53ef\u4ee5\u6307\u5b9a\u67d0\u4e2a\u7c7b\u5fc5\u987b\u5b9e\u73b0\u54ea\u4e9b\u65b9\u6cd5\uff0c\u4f46\u4e0d\u9700\u8981\u5b9a\u4e49\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5185\u5bb9\u3002<\/p>\n<p>\u63a5\u53e3\u662f\u901a\u8fc7 <strong>interface<\/strong> \u5173\u952e\u5b57\u6765\u5b9a\u4e49\u7684\uff0c\u5c31\u50cf\u5b9a\u4e49\u4e00\u4e2a\u6807\u51c6\u7684\u7c7b\u4e00\u6837\uff0c\u4f46\u5176\u4e2d\u5b9a\u4e49\u6240\u6709\u7684\u65b9\u6cd5\u90fd\u662f\u7a7a\u7684\u3002<\/p>\n<p>\u63a5\u53e3\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u65b9\u6cd5\u90fd\u5fc5\u987b\u662f\u516c\u6709\uff0c\u8fd9\u662f\u63a5\u53e3\u7684\u7279\u6027\u3002<\/p>\n<p>\u8981\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53e3\uff0c\u4f7f\u7528 <strong>implements<\/strong> \u64cd\u4f5c\u7b26\u3002\u7c7b\u4e2d\u5fc5\u987b\u5b9e\u73b0\u63a5\u53e3\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u5426\u5219\u4f1a\u62a5\u4e00\u4e2a\u81f4\u547d\u9519\u8bef\u3002\u7c7b\u53ef\u4ee5\u5b9e\u73b0\u591a\u4e2a\u63a5\u53e3\uff0c\u7528\u9017\u53f7\u6765\u5206\u9694\u591a\u4e2a\u63a5\u53e3\u7684\u540d\u79f0\u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\n    \/\/ \u58f0\u660e\u4e00\u4e2a&#039;iTemplate&#039;\u63a5\u53e3\n    interface iTemplate\n{\n    public function setVariable($name, $var);\n    public function getHtml($template);\n}\n\/\/ \u5b9e\u73b0\u63a5\u53e3\nclass Template implements iTemplate\n{\n    private $vars = array();\n\n    public function setVariable($name, $var)\n    {\n        $this-&gt;vars[$name] = $var;\n    }\n\n    public function getHtml($template)\n    {\n        foreach($this-&gt;vars as $name =&gt; $value) {\n            $template = str_replace(&#039;{&#039; . $name . &#039;}&#039;, $value, $template);\n        }\n\n        return $template;\n    }\n}<\/code><\/pre>\n<h3>\u62bd\u8c61\u7c7b<\/h3>\n<p>\u4efb\u4f55\u4e00\u4e2a\u7c7b\uff0c\u5982\u679c\u5b83\u91cc\u9762\u81f3\u5c11\u6709\u4e00\u4e2a\u65b9\u6cd5\u662f\u88ab\u58f0\u660e\u4e3a\u62bd\u8c61\u7684\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7c7b\u5c31\u5fc5\u987b\u88ab\u58f0\u660e\u4e3a\u62bd\u8c61\u7684\u3002<\/p>\n<p>\u5b9a\u4e49\u4e3a\u62bd\u8c61\u7684\u7c7b\u4e0d\u80fd\u88ab\u5b9e\u4f8b\u5316\u3002<\/p>\n<p>\u88ab\u5b9a\u4e49\u4e3a\u62bd\u8c61\u7684\u65b9\u6cd5\u53ea\u662f\u58f0\u660e\u4e86\u5176\u8c03\u7528\u65b9\u5f0f\uff08\u53c2\u6570\uff09\uff0c\u4e0d\u80fd\u5b9a\u4e49\u5176\u5177\u4f53\u7684\u529f\u80fd\u5b9e\u73b0\u3002<\/p>\n<p>\u7ee7\u627f\u4e00\u4e2a\u62bd\u8c61\u7c7b\u7684\u65f6\u5019\uff0c\u5b50\u7c7b\u5fc5\u987b\u5b9a\u4e49\u7236\u7c7b\u4e2d\u7684\u6240\u6709\u62bd\u8c61\u65b9\u6cd5\uff1b\u53e6\u5916\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u7684\u8bbf\u95ee\u63a7\u5236\u5fc5\u987b\u548c\u7236\u7c7b\u4e2d\u4e00\u6837\uff08\u6216\u8005\u66f4\u4e3a\u5bbd\u677e\uff09\u3002\u4f8b\u5982\u67d0\u4e2a\u62bd\u8c61\u65b9\u6cd5\u88ab\u58f0\u660e\u4e3a\u53d7\u4fdd\u62a4\u7684\uff0c\u90a3\u4e48\u5b50\u7c7b\u4e2d\u5b9e\u73b0\u7684\u65b9\u6cd5\u5c31\u5e94\u8be5\u58f0\u660e\u4e3a\u53d7\u4fdd\u62a4\u7684\u6216\u8005\u516c\u6709\u7684\uff0c\u800c\u4e0d\u80fd\u5b9a\u4e49\u4e3a\u79c1\u6709\u7684\u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\nabstract class AbstractClass\n{\n \/\/ \u5f3a\u5236\u8981\u6c42\u5b50\u7c7b\u5b9a\u4e49\u8fd9\u4e9b\u65b9\u6cd5\n    abstract protected function getValue();\n    abstract protected function prefixValue($prefix);\n\n    \/\/ \u666e\u901a\u65b9\u6cd5\uff08\u975e\u62bd\u8c61\u65b9\u6cd5\uff09\n    public function printOut() {\n        print $this-&gt;getValue() . PHP_EOL;\n    }\n}<\/code><\/pre>\n<h3>Static \u5173\u952e\u5b57<\/h3>\n<ul>\n<li>\u58f0\u660e\u7c7b\u5c5e\u6027\u6216\u65b9\u6cd5\u4e3a static(\u9759\u6001)\uff0c\u5c31\u53ef\u4ee5\u4e0d\u5b9e\u4f8b\u5316\u7c7b\u800c\u76f4\u63a5\u8bbf\u95ee\u3002<\/li>\n<li>\u9759\u6001\u5c5e\u6027\u4e0d\u80fd\u901a\u8fc7\u4e00\u4e2a\u7c7b\u5df2\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\u6765\u8bbf\u95ee\uff08\u4f46\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\uff09\u3002<\/li>\n<li>\u7531\u4e8e\u9759\u6001\u65b9\u6cd5\u4e0d\u9700\u8981\u901a\u8fc7\u5bf9\u8c61\u5373\u53ef\u8c03\u7528\uff0c\u6240\u4ee5\u4f2a\u53d8\u91cf $this \u5728\u9759\u6001\u65b9\u6cd5\u4e2d\u4e0d\u53ef\u7528\u3002<\/li>\n<li>\u9759\u6001\u5c5e\u6027\u4e0d\u53ef\u4ee5\u7531\u5bf9\u8c61\u901a\u8fc7 -&gt; \u64cd\u4f5c\u7b26\u6765\u8bbf\u95ee\u3002<\/li>\n<li>\u81ea PHP 5.3.0 \u8d77\uff0c\u53ef\u4ee5\u7528\u4e00\u4e2a\u53d8\u91cf\u6765\u52a8\u6001\u8c03\u7528\u7c7b\u3002\u4f46\u8be5\u53d8\u91cf\u7684\u503c\u4e0d\u80fd\u4e3a\u5173\u952e\u5b57 self\uff0cparent \u6216 static\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-php\">&lt;?php\nclass Foo {\n  public static $my_static = &#039;foo&#039;;\n  public function staticValue() {\n     return self::$my_static;\n  }\n}\nprint Foo::$my_static . PHP_EOL;\n$foo = new Foo();\nprint $foo-&gt;staticValue() . PHP_EOL;\n?&gt;    \n\/\/foo\n\/\/foo<\/code><\/pre>\n<h3>Final \u5173\u952e\u5b57<\/h3>\n<p>PHP 5 \u65b0\u589e\u4e86\u4e00\u4e2a final \u5173\u952e\u5b57\u3002\u5982\u679c\u7236\u7c7b\u4e2d\u7684\u65b9\u6cd5\u88ab\u58f0\u660e\u4e3a final\uff0c\u5219\u5b50\u7c7b\u65e0\u6cd5\u8986\u76d6\u8be5\u65b9\u6cd5\u3002\u5982\u679c\u4e00\u4e2a\u7c7b\u88ab\u58f0\u660e\u4e3a final\uff0c\u5219\u4e0d\u80fd\u88ab\u7ee7\u627f\u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\nclass BaseClass {\n   public function test() {\n       echo &quot;BaseClass::test() called&quot; . PHP_EOL;\n   }\n\n   final public function moreTesting() {\n       echo &quot;BaseClass::moreTesting() called&quot;  . PHP_EOL;\n   }\n}\n\nclass ChildClass extends BaseClass {\n   public function moreTesting() {\n       echo &quot;ChildClass::moreTesting() called&quot;  . PHP_EOL;\n   }\n}\n\/\/ \u62a5\u9519\u4fe1\u606f Fatal error: Cannot override final method BaseClass::moreTesting()\n?&gt;<\/code><\/pre>\n<h3>\u8c03\u7528\u7236\u7c7b\u6784\u9020\u65b9\u6cd5<\/h3>\n<p>PHP \u4e0d\u4f1a\u5728\u5b50\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u81ea\u52a8\u7684\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u3002\u8981\u6267\u884c\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u9700\u8981\u5728\u5b50\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u8c03\u7528 <strong>parent::__construct()<\/strong> \u3002<\/p>\n<pre><code class=\"language-php\">&lt;?php\nclass BaseClass {\n   function __construct() {\n       print &quot;BaseClass \u7c7b\u4e2d\u6784\u9020\u65b9\u6cd5&quot; . PHP_EOL;\n   }\n}\nclass SubClass extends BaseClass {\n   function __construct() {\n       parent::__construct();  \/\/ \u5b50\u7c7b\u6784\u9020\u65b9\u6cd5\u4e0d\u80fd\u81ea\u52a8\u8c03\u7528\u7236\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\n       print &quot;SubClass \u7c7b\u4e2d\u6784\u9020\u65b9\u6cd5&quot; . PHP_EOL;\n   }\n}\nclass OtherSubClass extends BaseClass {\n    \/\/ \u7ee7\u627f BaseClass \u7684\u6784\u9020\u65b9\u6cd5\n}\n\n\/\/ \u8c03\u7528 BaseClass \u6784\u9020\u65b9\u6cd5\n$obj = new BaseClass();\n\n\/\/ \u8c03\u7528 BaseClass\u3001SubClass \u6784\u9020\u65b9\u6cd5\n$obj = new SubClass();\n\n\/\/ \u8c03\u7528 BaseClass \u6784\u9020\u65b9\u6cd5\n$obj = new OtherSubClass();\n?&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u547d\u540d\u7a7a\u95f4 namespace PHP \u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u89e3\u51b3\u4ee5\u4e0b\u4e24\u7c7b\u95ee\u9898\uff1a \u7528\u6237\u7f16\u5199\u7684\u4ee3\u7801\u4e0ePHP\u5185\u90e8\u7684\u7c7b\/\u51fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":122,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-219","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-31"],"_links":{"self":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":1,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":220,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/219\/revisions\/220"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/media\/122"}],"wp:attachment":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}