Home >Backend Development >PHP Problem >How to load another class file in php

How to load another class file in php

WJ
WJOriginal
2020-06-10 16:35:422872browse

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 = &#39;zhouqi&#39;;
        public function say()
        {
            echo &#39;hello &#39;.$this->name;
        }
    }
<?php
    class b
    {
        //require(&#39;a.php&#39;); 错误,类中不能直接包含文件,但是可以在类定义的外部或类的方法中包含文件
        //$c = new a();      错误,类中不能直接实例化另外一个类,应该在类中的方法中实例化另一个类
        public function usea(){
            //包含a.php和实例化class a
            require (&#39;a.php&#39;);
            $person= new a();
            $person->name = &#39;erbao&#39;;
            $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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn