dongyan1899 2015-06-11 05:04 采纳率: 100%
浏览 380
已采纳

如何在laravel中使用带有命名空间的include_once()

I am new in laravel and importing a existing php site. I created a controller named "List" then i need to create a object of a class, coded in a file which is been include by include_once() as shown,

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;


$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");

class Lists extends Controller
{
    public function show()
    {
        $objServiceDetails= new ServiceDetails;
        .........
        ........
     }
 }

But i am getting an error like

Fatal error: Class 'App\Http\Controllers\ServiceDetails' not found

I dont have much idea of namespace "use" and "as". May be that's why i am not able to solve this problem. When creating a new object it is searching class in namespace location only, but it should also look in included files, i think.

  • 写回答

2条回答 默认 最新

  • douxian6260 2015-06-11 05:20
    关注

    If there's a namespace in current file, PHP will try to find class in current namespace and if it won't find it, you'll get fatal error. You should open ServiceDetails.class.php class to verify if there is namespace ...; at the beginning of the file (after <?php). If not you can simple add in your Lists file after:

    use App\Http\Controllers\Controller;
    

    the following line:

    use ServiceDetails
    

    and if it is, you should copy that namespace and add the following line:

    use namespaceyoucopied\ServiceDetails;
    

    of course in namespaceyoucopied place you need to put the correct copied namespace so it could look like this:

    use A\B\C\ServiceDetails;
    

    You can also look at How to use objects from other namespaces and how to import namespaces in PHP or PHP namespaces manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部