dongyan1899 2015-06-11 13:04 采纳率: 100%
浏览 375
已采纳

如何在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 13: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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度