# 魔法函数 1。魔术函数,不需要主动去调用, 进行一些操作的时候,会自动调用 1. \_\_construct(),创建一个对象里,被调用 2. \_\_call(), 调用一个不可访问方法时调用 3. \_\_callStatic(),用静态方式中调用一个不可访问方法时调用 4. \_\_get(),一个类不可访问的属性时调用 5. \_\_set(),设置一个类不可以访问的属性时调用 6.等等 2。说下最常用的 \_construct吧。 ``` <pre class="calibre11">``` class Book { public $name; protected $costPrice; public function __construct($name, $costPrice) { $this->name = $name; $this->costPrice = $costPrice; } public function save() { print_r("保存完毕: {$this->name} {$this->costPrice}"); } } // new 的时候,会自动调用 __construct 方法,并会值传递给它 $book = new Book('php编程', '9.9元'); $book->save(); ``` ```