企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 第七课 面向对象 > 定义对象 ``` <pre class="calibre14">``` class <span class="token1">Book</span><span class="token2">{</span> const PI<span class="token">=</span><span class="token3">3.14</span><span class="token2">;</span><span class="token6">//常量</span> private $name<span class="token2">;</span><span class="token6">//私有不可继承</span> public $sex<span class="token2">;</span> <span class="token5">function</span> <span class="token1">__construct</span><span class="token2">(</span>$name<span class="token2">,</span>$sex<span class="token2">)</span><span class="token2">{</span><span class="token6">//构造方法</span> $this<span class="token">-</span><span class="token">></span><span class="token1">name</span><span class="token2">(</span>$name<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">function</span> <span class="token1">__destruct</span><span class="token2">(</span><span class="token2">)</span><span class="token2">{</span> echo <span class="token4">"析构方法."</span><span class="token2">;</span> <span class="token2">}</span> public <span class="token5">function</span> <span class="token1">name</span><span class="token2">(</span>$name<span class="token2">)</span><span class="token2">{</span><span class="token6">//设置</span> $this<span class="token">-</span><span class="token">></span>name <span class="token">=</span> $name<span class="token2">;</span> <span class="token5">return</span> $this<span class="token">-</span><span class="token">></span>name<span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 继承 ``` <pre class="calibre14">``` class <span class="token1">A</span> extends <span class="token1">B</span><span class="token2">{</span> <span class="token2">}</span> ``` ``` parent 调用父类 self 调用当前类静态方法和变量,在静态方法中,必须用self::调用本类中方法 public 公共的 private 私有,本类中调用,子类不可继承 protected受保护,子类可调用 命名空间 namespace logoove\\qq; 命名空间内使用调用类 use logoove\\qq\\类名; 命名空间内如果调用未使用空间命名类 require\_once加载 类名前加\\ 比如 new \\phpExcel()