{"id":33,"date":"2024-08-14T15:11:01","date_gmt":"2024-08-14T07:11:01","guid":{"rendered":"https:\/\/www.linhw.cn\/?p=33"},"modified":"2024-08-14T15:11:01","modified_gmt":"2024-08-14T07:11:01","slug":"php-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%9a%84%e7%90%86%e8%a7%a3%ef%bc%8c%e4%bb%a5%e5%8f%8a-php-%e6%98%af%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%9a%84","status":"publish","type":"post","link":"https:\/\/www.linhw.cn\/index.php\/2024\/08\/14\/php-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%9a%84%e7%90%86%e8%a7%a3%ef%bc%8c%e4%bb%a5%e5%8f%8a-php-%e6%98%af%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%9a%84\/","title":{"rendered":"PHP \u9762\u5411\u5bf9\u8c61\u7684\u7406\u89e3\uff0c\u4ee5\u53ca PHP \u662f\u5982\u4f55\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><strong>\u4e00\u3001 \u5bf9 PHP \u9762\u5411\u5bf9\u8c61\u7684\u7406\u89e3<\/strong><\/p>\n\n\n\n<p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u662f\u4e00\u79cd\u8f6f\u4ef6\u5f00\u53d1\u8303\u5f0f\uff0c\u5b83\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u201c\u5bf9\u8c61\u201d\u3002 \u8fd9\u4e9b\u5bf9\u8c61\u662f\u7c7b\u7684\u5b9e\u4f8b\uff0c\u7c7b\u662f\u5bf9\u8c61\u7684\u84dd\u56fe\u3002PHP \u4ece PHP5 \u5f00\u59cb\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u5e76\u9010\u6e10\u5b8c\u5584\u3002<\/p>\n\n\n\n<p><strong>OOP \u7684\u6838\u5fc3\u6982\u5ff5\u5305\u62ec\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7c7b\uff08Class\uff09\uff1a<\/strong> \u5bf9\u8c61\u7684\u84dd\u56fe\uff0c\u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u5bf9\u8c61\uff08Object\uff09\uff1a<\/strong> \u7c7b\u7684\u5b9e\u4f8b\uff0c\u62e5\u6709\u7c7b\u5b9a\u4e49\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u5c01\u88c5\uff08Encapsulation\uff09\uff1a<\/strong> \u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u5c01\u88c5\u5728\u5bf9\u8c61\u5185\u90e8\uff0c\u9690\u85cf\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u5bf9\u5916\u63d0\u4f9b\u63a5\u53e3\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>\u7ee7\u627f\uff08Inheritance\uff09\uff1a<\/strong> \u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u548c\u6269\u5c55\u3002<\/li>\n\n\n\n<li><strong>\u591a\u6001\uff08Polymorphism\uff09\uff1a<\/strong> \u540c\u4e00\u4e2a\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u7c7b\u4e2d\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>OOP \u7684\u4f18\u52bf:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4ee3\u7801\u91cd\u7528\u6027\u9ad8:<\/strong> \u901a\u8fc7\u7ee7\u627f\u548c\u591a\u6001\uff0c\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u7ef4\u62a4\u6027\u5f3a:<\/strong> \u5c01\u88c5\u6027\u4f7f\u5f97\u4ee3\u7801\u6613\u4e8e\u7ef4\u62a4\u548c\u4fee\u6539\uff0c\u964d\u4f4e\u6a21\u5757\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u6269\u5c55\u6027\u597d:<\/strong> \u6613\u4e8e\u6dfb\u52a0\u65b0\u529f\u80fd\uff0c\u800c\u65e0\u9700\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4e8c\u3001 PHP \u5982\u4f55\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<p>PHP \u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u6765\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u5e76\u652f\u6301\u5c01\u88c5\u3001\u7ee7\u627f\u3001\u591a\u6001\u7b49\u7279\u6027\u3002<\/p>\n\n\n\n<p><strong>1. \u7c7b\u548c\u5bf9\u8c61:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 class \u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\uff0c\u7c7b\u4e2d\u53ef\u4ee5\u5305\u542b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 new \u5173\u952e\u5b57\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      <code>class Car { \/\/ \u5b9a\u4e49\u4e00\u4e2a Car \u7c7b\n    public $brand; \/\/ \u516c\u5171\u5c5e\u6027\n    protected $model; \/\/ \u53d7\u4fdd\u62a4\u7684\u5c5e\u6027\n    private $price; \/\/ \u79c1\u6709\u5c5e\u6027\n\n    public function __construct($brand, $model, $price) { \/\/ \u6784\u9020\u51fd\u6570\n        $this-&gt;brand = $brand;\n        $this-&gt;model = $model;\n        $this-&gt;price = $price;\n    }\n\n    public function getBrand() { \/\/ \u516c\u5171\u65b9\u6cd5\n        return $this-&gt;brand;\n    }\n}\n\n$myCar = new Car(\"Toyota\", \"Camry\", 25000); \/\/ \u5b9e\u4f8b\u5316\u4e00\u4e2a Car \u5bf9\u8c61\necho $myCar-&gt;getBrand(); \/\/ \u8f93\u51fa: Toyota<\/code>\n    <\/pre>\n\n\n\n<p><strong>2. \u5c01\u88c5:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP \u4f7f\u7528 public\u3001protected \u548c private \u4e09\u4e2a\u8bbf\u95ee\u4fee\u9970\u7b26\u6765\u63a7\u5236\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u5b9e\u73b0\u5c01\u88c5\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>3. \u7ee7\u627f:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 extends \u5173\u952e\u5b57\u5b9e\u73b0\u7ee7\u627f\u3002<\/li>\n\n\n\n<li>\u5b50\u7c7b\u53ef\u4ee5\u7ee7\u627f\u7236\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5e76\u53ef\u4ee5\u6dfb\u52a0\u81ea\u5df1\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>PHP \u53ea\u652f\u6301\u5355\u7ee7\u627f\uff0c\u4e00\u4e2a\u5b50\u7c7b\u53ea\u80fd\u7ee7\u627f\u4e00\u4e2a\u7236\u7c7b\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      <code>class SportsCar extends Car { \/\/ SportsCar \u7c7b\u7ee7\u627f Car \u7c7b\n    public $speed;\n\n    public function __construct($brand, $model, $price, $speed) {\n        parent::__construct($brand, $model, $price); \/\/ \u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\n        $this-&gt;speed = $speed;\n    }\n}\n\n$mySportsCar = new SportsCar(\"Ferrari\", \"458 Italia\", 350000, 200); \necho $mySportsCar-&gt;getBrand(); \/\/ \u8f93\u51fa: Ferrari\uff0c\u7ee7\u627f\u81ea\u7236\u7c7b<\/code>\n    <\/pre>\n\n\n\n<p><strong>4. \u591a\u6001:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP \u901a\u8fc7\u63a5\u53e3\u548c\u62bd\u8c61\u7c7b\u5b9e\u73b0\u591a\u6001\u3002<\/li>\n\n\n\n<li>\u63a5\u53e3\u4f7f\u7528 interface \u5173\u952e\u5b57\u5b9a\u4e49\uff0c\u53ea\u80fd\u5305\u542b\u65b9\u6cd5\u58f0\u660e\uff0c\u4e0d\u80fd\u5305\u542b\u5c5e\u6027\u548c\u65b9\u6cd5\u5b9e\u73b0\u3002<\/li>\n\n\n\n<li>\u62bd\u8c61\u7c7b\u4f7f\u7528 abstract \u5173\u952e\u5b57\u5b9a\u4e49\uff0c\u53ef\u4ee5\u5305\u542b\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f46\u62bd\u8c61\u65b9\u6cd5\u5fc5\u987b\u7531\u5b50\u7c7b\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u793a\u4f8b:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      <code>interface Drivable { \/\/ \u5b9a\u4e49\u4e00\u4e2a Drivable \u63a5\u53e3\n    public function drive();\n}\n\nclass Car implements Drivable { \/\/ Car \u7c7b\u5b9e\u73b0 Drivable \u63a5\u53e3\n    public function drive() {\n        echo \"Driving a car\";\n    }\n}\n\nclass Bike implements Drivable { \/\/ Bike \u7c7b\u5b9e\u73b0 Drivable \u63a5\u53e3\n    public function drive() {\n        echo \"Riding a bike\";\n    }\n}<\/code>\n    <\/pre>\n\n\n\n<p><strong>\u603b\u7ed3:<\/strong><\/p>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8bed\u6cd5\u548c\u7279\u6027\u6765\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u5408\u7406\u5730\u8fd0\u7528 OOP \u539f\u5219\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002\u5bf9\u4e8e\u62e5\u6709 \u591a \u5e74 PHP \u5f00\u53d1\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u6df1\u5165\u7406\u89e3 OOP \u7684\u6982\u5ff5\u548c PHP \u7684\u5b9e\u73b0\u673a\u5236\uff0c\u5e76\u80fd\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u8fd0\u7528\uff0c\u662f\u5fc5\u5907\u7684\u6280\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001 \u5bf9 PHP \u9762\u5411\u5bf9\u8c61\u7684\u7406\u89e3 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u662f\u4e00\u79cd\u8f6f\u4ef6\u5f00\u53d1\u8303\u5f0f\uff0c\u5b83\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u6cd5\u5c01\u88c5\u5728\u4e00\u8d77 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.linhw.cn\/index.php\/2024\/08\/14\/php-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%9a%84%e7%90%86%e8%a7%a3%ef%bc%8c%e4%bb%a5%e5%8f%8a-php-%e6%98%af%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%9a%84\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cPHP \u9762\u5411\u5bf9\u8c61\u7684\u7406\u89e3\uff0c\u4ee5\u53ca PHP \u662f\u5982\u4f55\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-php","category-9"],"_links":{"self":[{"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":1,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linhw.cn\/index.php\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}