{"id":610,"date":"2023-03-22T04:22:36","date_gmt":"2023-03-22T04:22:36","guid":{"rendered":"http:\/\/www.nywzjs.com\/?p=610"},"modified":"2023-03-22T04:23:30","modified_gmt":"2023-03-22T04:23:30","slug":"php-protected-%e5%b1%9e%e6%80%a7php-%e7%b1%bb-%e5%b1%9e%e6%80%a7%e5%92%8c%e6%96%b9%e6%b3%95%e7%9a%84%e5%85%b3%e7%b3%bb","status":"publish","type":"post","link":"http:\/\/www.nywzjs.com\/?p=610","title":{"rendered":"php protected \u5c5e\u6027,php \u7c7b \u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5173\u7cfb"},"content":{"rendered":"\n<p>\u4e00 . \u5c5e\u6027\u5173\u7cfb<\/p>\n\n\n\n<p>2 .\u00a0\u00a0protected\u5c5e\u6027\uff1a\u5728\u7c7b\u7684\u5185\u90e8\u53ef\u4ee5\u8c03\u7528\u5916\u90e8\u4e0d\u80fd\u53ef\u4ee5\u88ab\u7ee7\u627f\u5e76\u4e14\u91cd\u6784<\/p>\n\n\n\n<p>1 .&nbsp; public\u5c5e\u6027\uff1a\u5728\u7c7b\u7684\u5916\u90e8\u548c\u5185\u90e8\u90fd\u53ef\u4ee5\u8c03\u7528&nbsp; \u88ab\u7ee7\u627f\u80fd\u591f\u91cd\u6784<\/p>\n\n\n\n<p>3 .\u00a0\u00a0private\u5c5e\u6027\uff1a\u53ea\u6709\u5728\u7c7b\u7684\u5185\u90e8\u8c03\u7528 \u4e0d\u80fd\u88ab\u7ee7\u627f<\/p>\n\n\n\n<p>4 .\u00a0\u00a0static\u5c5e\u6027\uff1a\u7c7b\u7684\u5185\u90e8\u548c\u5916\u90e8\u90fd\u80fd\u8c03\u7528\u00a0 \u5185\u90e8\u8c03\u7528\u65b9\u6cd5(self\uff1a\uff1a\u5c5e\u6027\u53d8\u91cf) \u5916\u90e8\u8c03\u7528\u65b9\u6cd5(\u7c7b\u540d\uff1a\uff1a\u5c5e\u6027\u53d8\u91cf)<\/p>\n\n\n\n<p>\u4e8c . \u65b9\u6cd5\u5173\u7cfb<\/p>\n\n\n\n<p>1 .&nbsp;public\u65b9\u6cd5\uff1a\u5728\u7c7b\u7684\u5916\u90e8\u548c\u5185\u90e8\u90fd\u53ef\u4ee5\u8c03\u7528 \u53ef\u4ee5\u88ab\u7ee7\u627f<\/p>\n\n\n\n<p>2 .&nbsp;protected\u65b9\u6cd5\uff1a\u5728\u7c7b\u7684\u5185\u90e8\u53ef\u4ee5\u8c03\u7528\u5916\u90e8\u4e0d\u80fd \u53ef\u4ee5\u88ab\u7ee7\u627f<\/p>\n\n\n\n<p>3 .&nbsp; private\u65b9\u6cd5\uff1a\u53ea\u6709\u5728\u7c7b\u7684\u5185\u90e8\u8c03\u7528 \u4e0d\u53ef\u4ee5\u88ab\u7ee7\u627f<\/p>\n\n\n\n<p>4 .&nbsp;static\u65b9\u6cd5\uff1a\u5916\u90e8\u5185\u90e8\u90fd\u53ef\u4ee5\u8c03\u7528&nbsp; \u5185\u90e8\u8c03\u7528\u65b9\u6cd5($this-&gt;\u65b9\u6cd5\u540d) \u5916\u90e8\u8c03\u7528\u53d1\u6cd5(\u7c7b\u540d\uff1a\uff1a\u65b9\u6cd5\u540d)<\/p>\n\n\n\n<p>\u4e09 . final \u8bf4\u660e<\/p>\n\n\n\n<p>1 . final---\u7528\u4e8e\u7c7b\u3001\u65b9\u6cd5\u524d\u3002<\/p>\n\n\n\n<p>2 .&nbsp;final\u7c7b---\u4e0d\u53ef\u88ab\u7ee7\u627f\u3002<\/p>\n\n\n\n<p>3 . final\u65b9\u6cd5---\u4e0d\u53ef\u88ab\u8986\u76d6\u3002<\/p>\n\n\n\n<p>\u56db . \u4e0b\u9762\u662f\u81ea\u5df1\u5199\u7684\u6d4b\u8bd5demo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\/*\r\n* DEFINE\u3000MyClass1\r\n* Date 2019-3-14\r\n*\/\r\nclass MyClass1 { \/\/\u58f0\u660e\u4e00\u4e2a\u516c\u5171\u7684\u53d8\u91cf\r\n    public $public = 'Public'; \/\/\u58f0\u660e\u4e00\u4e2a\u53d7\u4fdd\u62a4\u7684\u53d8\u91cf\r\n    protected $protected = 'Protected'; \/\/\u58f0\u660e\u4e00\u4e2a\u79c1\u6709\u7684\u53d8\u91cf\r\n    private $private = 'Private';\r\n\r\n    function printAll()\r\n\r\n    {\r\n        echo $this->public;\r\n        echo $this->protected;\r\n        echo $this->private;\r\n\r\n    }\r\n\r\n} \/\/\u5b9e\u4f8b\u5316\u5bf9\u8c61\r\n\r\n$obj = newMyClass1();\r\necho $obj->public; \/\/\u6b63\u5e38\u8f93\u51fa\uff1a Public\r\necho $obj->protected; \/\/\u62a5\u9519\uff1aCannot access protected property\r\necho $obj->private; \/\/\u62a5\u9519\uff1aCannot access protected property\r\n$obj->printAll(); \/\/\u6b63\u5e38\u8f93\u51fa Public Protected Private\r\n\r\n\/**\r\n * DEFINE\u3000MyClass2\r\n * Date 2019-3-14\r\n *\/\r\n\r\nclass MyClass2 extends MyClass1 { \/\/\u53ef\u4ee5\u5bf9 public \u548c protected \u8fdb\u884c\u91cd\u5b9a\u4e49\uff0c\u4f46 private \u800c\u4e0d\u80fd\r\n\r\n    public $public = 'Public2';\r\n    protected $protected = 'Protected2';\r\n    static $a = 1;\r\n\r\n    function printAll() {\r\n        echo $this->public;\r\n        echo $this->protected;\r\n        echo $this->private;\r\n\r\n    }\r\n\r\n}\r\n$obj2 = newMyClass2();\r\necho $obj2->public; \/\/\u8fd9\u884c\u80fd\u88ab\u6b63\u5e38\u6267\u884c\r\n\r\necho $obj2->private; \/\/\u672a\u5b9a\u4e49 private\r\n\r\necho $obj2->protected; \/\/\u8fd9\u884c\u4f1a\u4ea7\u751f\u4e00\u4e2a\u81f4\u547d\u9519\u8bef\r\n\r\n$obj2->printAll(); \/\/\u8f93\u51fa Public Protected2 \u548c Undefined\r\n\r\n\/**\r\n\r\n*DEFINE MyClass3\r\n\r\n* \u7c7b\u4e2d\u65b9\u6cd5\u7684\u8bbf\u95ee\u63a7\u5236*\/\r\n\r\nclass MyClass3 { \/\/\u58f0\u660e\u4e00\u4e2a\u5171\u6709\u7684\u6784\u9020\u51fd\u6570\r\n\r\n    public\r\n    function __construct() {} \/\/\u58f0\u660e\u4e00\u4e2a\u5171\u6709\u7684\u65b9\u6cd5\r\n\r\n    public\r\n    function myPublic() {\r\n        return 'myPublic';\r\n\r\n    } \/\/\u58f0\u660e\u4e00\u4e2a\u53d7\u4fdd\u62a4\u7684\u65b9\u6cd5\r\n\r\n    protected\r\n    function myProtected() {\r\n        return 'myProtected';\r\n\r\n    } \/\/\u58f0\u660e\u4e00\u4e2a\u79c1\u6709\u7684\u65b9\u6cd5\r\n\r\n    private\r\n    function myPrivate() {\r\n        return 'myPrivate';\r\n\r\n    } \/\/\u68c0\u67e5\u8f93\u51fa\u6240\u6709\u65b9\u6cd5\r\n\r\n    public functionprintAll() {\r\n        echo $this->myPublic();\r\n        echo $this->myProtected();\r\n        echo $this->myPrivate();\r\n\r\n    }\r\n\r\n}\r\n$obj3 = newMyClass3;\r\necho $obj3->myPublic(); \/\/\u6b63\u5e38\u6267\u884c\r\n\r\necho $obj3->myProtected(); \/\/\u62a5\u9519\r\n\r\necho $obj3->myPrivate(); \/\/\u62a5\u9519\r\n\r\n$obj3->printAll(); \/\/\u4e09\u4e2a\u65b9\u6cd5\u5728\u7c7b\u7684\u5185\u90e8\u5747\u53ef\u6b63\u5e38\u6267\u884c\r\n\r\nclass Tests\r\n\r\n{\r\n    public\r\n    function test() {\r\n        $this->testPrivate();\r\n        $this->testPublic();\r\n        $this->testStatic();\r\n\r\n    }\r\n    public\r\n    function testPublic() {\r\n        echo \"1\";\r\n\r\n    }\r\n    private\r\n    function testPrivate() {\r\n        echo \"2\";\r\n\r\n    }\r\n    public static\r\n    function testStatic() {\r\n        echo 3;\r\n\r\n    }\r\n\r\n}\r\n$myFoo = newTests();\r\n\r\nTests::testStatic();\r\n$myFoo->test();\r\n\/**\r\n\r\n* Class Math\r\n\r\n* final \u89e3\u91ca*\/\r\n\r\nclass Math\r\n\r\n{\r\n    public\r\n    final\r\n\r\n    function max() {\r\n        echo 1;\r\n\r\n    }\r\n\r\n} \/\/\u58f0\u660e\u7c7bSuperMath \u7ee7\u627f\u81ea Math\u7c7b\r\n\r\nclass SuperMath extends Math\r\n\r\n{\r\n    public\r\n    final\r\n\r\n    function max() {\r\n        echo 2;\r\n\r\n    }\r\n\r\n} \/\/\u6267\u884c\u4f1a\u51fa\u9519\uff0cfinal\u65b9\u6cd5\u4e0d\u80fd\u88ab\u91cd\u5199\u3002\r\n\r\n?><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u6587\u7ae0\u6765\u6e90<\/p><cite>https:\/\/blog.csdn.net\/weixin_32918953\/article\/details\/115115438<\/cite><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00 . \u5c5e\u6027\u5173\u7cfb 2 .\u00a0\u00a0protected\u5c5e\u6027\uff1a\u5728\u7c7b\u7684\u5185\u90e8\u53ef\u4ee5\u8c03\u7528\u5916\u90e8\u4e0d\u80fd\u53ef\u4ee5\u88ab\u7ee7\u627f\u5e76\u4e14\u91cd\u6784 1 .&#038;nb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":450,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,11],"tags":[],"_links":{"self":[{"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/posts\/610"}],"collection":[{"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=610"}],"version-history":[{"count":0,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=\/wp\/v2\/media\/450"}],"wp:attachment":[{"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.nywzjs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}