【问题标题】:setting function path in php?在php中设置函数路径?
【发布时间】:2014-10-18 14:55:09
【问题描述】:

我在文件夹 (dirC) 中有 gcm...这里是路径

index.php
dirA/    
dirB/    
dirC/GCM.php    
----/config.php
----/sendMsg.php
dirD/
dirE/dirE1/test.php            //send from here
dirF/

gcm.php 代码如下

class GCM {

    function __construct() {

    }

    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {
        // include config
        include_once './config.php';
          //other code here
        echo 'test';

     }
}

我成功地从 sendMsg.php 发送消息,这里是代码

include_once './GCM.php';
    $message="hello word";
    $gcm = new GCM();
    $registatoin_ids = array("SEJFOJOEUJFPUWPJR0923740JEU092308UPUPUAUOJLJLJJVPW634");
    $message = array($message);

    $result = $gcm->send_notification($registatoin_ids, $message);
    echo $result;

我的问题是如何设置路径使用 test.php 的 include_once 与 GCM.php 通信?

这里是test.php代码

include_once './GCM.php'; //my proble is here ???????????
$message="hello word";
$gcm = new GCM();
$registatoin_ids = array("SEJFOJOEUJFPUWPJR0923740JEU092308UPUPUAUOJLJLJJVPW634");
$message = array($message);

$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;

提前谢谢..

【问题讨论】:

  • 如果 GCM.php 在另一个目录中,您可以使用 ../ 浏览到该目录(向上一个目录)。
  • 2point (../) 和 (./) 有什么区别?
  • ../ 是上移一个目录 ./ 是当前目录

标签: php path include include-path


【解决方案1】:

当你想到达你想要的目录时,你需要使用../,意思是“上移一个目录”。

示例: 我们有一个目录结构:

/website
/website/include
/website/template

假设我们在 /website/template 目录中。 如果我们想包含 /website/include/GCM.php

我们可以用绝对路径,/表示根目录:

include_once '/website/include/GCM.php'; 

我们可以使用相对路径:

include_once '../include/GCM.php';

../ 表示“上移 1 个目录”。

欲了解更多信息:http://www.geeksengine.com/article/absolute-relative-path.html

【讨论】:

    【解决方案2】:

    这个问题不是很清楚。但我认为问题在于您创建了一个文件夹,从中包含并且您不知道那是什么文件夹?

    为此,您在 php 中有魔术常量: http://php.net/manual/en/language.constants.predefined.php

    【讨论】:

      【解决方案3】:

      从 php 文档中你可以做类似的事情:

      $path = '/dirC';
      set_include_path(get_include_path() . PATH_SEPARATOR . $path);
      

      在您的 index.php(如果它是您的控制器)或 conf/prepend 文件中,该文件包含在您的所有脚本中。

      然后一个简单的include('GCM.php'); 就可以了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 1970-01-01
        • 1970-01-01
        • 2017-01-24
        • 2013-04-30
        • 2011-06-06
        • 1970-01-01
        相关资源
        最近更新 更多