【问题标题】:using same namespace php, Call to undefined function使用相同的命名空间 php,调用未定义的函数
【发布时间】:2014-02-27 00:14:32
【问题描述】:

使用相同的命名空间 php

我在同一个文件夹中有这些文件:

OtherFunctions.php

<?php
namespace Pack\sp;
$Tble = NULL;

function SetTble($tble) {
  global $Tble;
  $Tble = $tble;
}

function GetTble() {
  global $Tble;
  return $Tble;
}

function Funct0($Str0, $Str1) {
  return $Str0 == $Str1;
}

function Funct1($Arg) {
  return "The Value is ".$Arg;
}
//... from 0 to 16
function Funct16($Arg) {
  return "The Value is ".$Arg;
}
?>

如何调用该文件中包含的所有函数?

在一个类文件 SubClass.php 我有这个:

<?php
namespace Pack\sp;
class SubClass {
  public $CArg = "";
}
?>

在其他类文件LeadClass.php 我有这个:

<?php
namespace Pack\sp;
use \Pack\sp\SubClass;
require_once("OtherFunctions.php");
class LeadClass {
  public function __construct($Name) {
    echo("_._");
    $NewSC = new SubClass();
    $NewSC->CArg = $Name;
    SetTble($Name);
    echo("ini:".GetTble().":end");
  }
}
?>

我想在OtherFunctions.php文件的一条指令中调用所有函数,但我不知道怎么做......

我试图在其他代码中复制此消息 致命错误:在第 10 行调用 C:...\LeadClass.php 中未定义的函数 GetTble()

但是,我得到的是空白页

编辑

被添加的行:

require_once("OtherFunctions.php"); 

并被换行:

require_once("SubClass.php");

按行:

use \Pack\sp\SubClass;

LeadClass.php 文件中。

但是,我得到的是空白页

【问题讨论】:

    标签: php class namespaces undefined


    【解决方案1】:

    你需要添加下一行

    namespace Pack\sp;
    use \Pack\sp\SubClass; // <--- add this
    

    另外我认为你应该把 OtherFunctions 文件的函数放到一个新的类链接中

    命名空间包\sp;

    class OtherFunctions{
      // your current code goes here
    }
    

    之后,您需要使用 OtherFunctios 类扩展子类

    namespace Pack\sp;
    use Pack\sp\OtherFunctions;
    class SubClass extends OtherFunctions {
      public $CArg = "";
    }
    

    编辑 我刚刚尝试了你的代码,我可以让 LeasClass 按如下方式工作

    <?php
    namespace Pack\sp;
    require_once("OtherFunctions.php");
    require_once("SubClass.php");
    class LeadClass {
      public function __construct($Name) {
        echo("_._");
        $NewSC = new SubClass();
        $NewSC->CArg = $Name;
        SetTble($Name);
        echo("ini:".GetTble().":end");
      }
    }
    
    $LeadClass = new LeadClass('table');
    
    ?>
    

    你已经初始化类了吗?

    【讨论】:

    • 我想使用不同于Class的OtherFunctions.php...因为它是一个只包含与类无关的函数的脚本。
    • 抱歉昨天来晚了,我刚刚编辑了我的答案,希望有用
    猜你喜欢
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2023-01-25
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 2012-01-21
    相关资源
    最近更新 更多