Home >Backend Development >PHP Problem >How to load another class file in php
#How does a php class file load another class file method?
There are a.php and b.php under the current file. I want to introduce class a into class b
<?php class a { public $name = 'zhouqi'; public function say() { echo 'hello '.$this->name; } }
<?php class b { //require('a.php'); 错误,类中不能直接包含文件,但是可以在类定义的外部或类的方法中包含文件 //$c = new a(); 错误,类中不能直接实例化另外一个类,应该在类中的方法中实例化另一个类 public function usea(){ //包含a.php和实例化class a require ('a.php'); $person= new a(); $person->name = 'erbao'; $person->say(); } } $person3 = new b(); $person3->usea();
Related recommendations: php tutorial
The above is the detailed content of How to load another class file in php. For more information, please follow other related articles on the PHP Chinese website!