Home  >  Q&A  >  body text

Small questions about the automatic loading of Dugu Jiujian class __autoload()

class class name{
function__autoload($ClassName){
$path=$ClassName.'.php';
if(file_exists($path)){//Judge whether the file exists
                                                                       
, I checked it several times, but I couldn’t see the statement meaning $ClassName =test. So how could he import text.php?

葫芦娃平衡盘葫芦娃平衡盘2600 days ago1105

reply all(2)I'll reply

  • 听装雪碧

    听装雪碧2017-08-09 11:34:00

    That is, when you create a new test class name, the __autoload method will be executed, and the class name test after your new will be passed into this method as a parameter

    reply
    0
  • 听装雪碧

    听装雪碧2017-08-09 11:30:43

    When you want to reference a non-existent class in this class, after writing the __autoload method, it will automatically determine whether the class file exists, and if it exists, it will be executed.

    require_once($path) refers to the class file

    You can add the following code to your file

    echo (new Autoload1())->god();

    echo (new Autoload1())->name;


    and then test.php Write

    class Test

    {

    public $name = 'I am the attribute name in the Test class file';

    public function god(){

    return 'I am the method god in the Test class file' ()';

    }

    }

    You can see that this Test file is automatically introduced and the value can be successfully output


    reply
    1
  • Cancelreply