Question

I'm trying to use RuntimeException

http://www.php.net/manual/en/class.runtimeexception.php

I have this files in the same folder :

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;
}
?>

How to call all functions contained in this file?

In one class File SubClass.php I have this:

<?php
namespace Pack\sp;
class SubClass {
  public $CArg = "";
  function FnClass($Arg) {
    return "FnClass:The Value is from SubClass".$Arg;
  }
}
?>

In other class File LeadClass.php I have this:

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

Using all code CallerNS.php

<?php
namespace Pack\sp;
require_once("LeadClass.php");
require_once("OtherFunctions.php");
$NewSC = new LeadClass("first");
$NewSC->getName();
if (Funct0("strings", "strings")) {
  print("same strings<br>");
}
?>

Here my error:

Fatal error: Class 'Pack\sp\RuntimeException' not found in C:...\LeadClass.php on line 15

In other version of CallerNS.php

<?php
use Pack\sp;
require_once("LeadClass.php");
require_once("OtherFunctions.php");
if (Funct0("strings", "strings")) {
  print("same strings<br>");
}
$NewSC = new LeadClass("first");
$NewSC->getName();
?>

The error message:

Fatal error: Call to undefined function Funct0() in C:...\CallerNS.php on line 5

Was it helpful?

Solution

As a standard answer, check your PHP version. The documentation for RuntimeException requires at least PHP 5.1 or higher.

You may do this via phpinfo(), or php -v in your console (if you are running it locally and have access to php via. PATH)

Edit: This question and answer seem related. Essentially, it is also your namespace causing the mixup. Essentially, it is looking for a namespaced RuntimeException class (see your error).

Since you're using namespaces, you will need to reference back to the root namespace (\) any time you want to use the native classes.

You may want to try:

throw new \RuntimeException('...'); // The '\' tells PHP to use the root namespace.

Second Edit:

As for your second issue, that is a matter of your code order. In your CallerNS.php file, you're declaring to use the namespace Pack\sp, and then including your namespaced files into your code. This won't work because you will need to have the namespace already declared before you can use it.

You can either include the files first, or use \Pack\sp\Funct0(...) instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top