Composer Gives Error, “Class Not Found”

让人想犯罪 __ 提交于 2019-12-01 21:15:12

In composer.json you defined that for src folder you use myns namespace, so in your childclass.php you should use

namespace myns;

It's also unnecessary to add:

require_once 'parentclass.php';

or

use myns\parentclass as parentclass;

so your childclass.php should look like this:

<?php

namespace myns;

class childclass extends parentclass
{
    public function abc()
    {
        echo 'hello world';
    }
}

In addition in run.php file you might replace:

use myns\childclass as childclass;

into

use myns\childclass;

You don't need to use as if you don't want to use other name for class.

You should also consider using namespaces with capital letter (Studly caps) and the same for classes. Instead of myns use MyNs, instead of parentclass use ParentClass. You should look at PSR-1 coding standard and PSR-2 coding standard to follow best coding practises.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!