Composer Gives Error, “Class Not Found”

后端 未结 1 1473
深忆病人
深忆病人 2021-01-19 03:48

I\'m using Windows 10. After making a folder src in the root directory I created two files in it.

Directory Structure (Before running composer ins

1条回答
  •  抹茶落季
    2021-01-19 03:55

    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:

    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.

    0 讨论(0)
提交回复
热议问题