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

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀