4

using same namespace 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 = "";
}
?>

In other class File LeadClass.php I have this:

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

I want call all function in one instruction of OtherFunctions.php File, but I don't kno how to do it....

I trying to replicate this message in other code Fatal error: Call to undefined function GetTble() in C:...\LeadClass.php on line 10

But, I'm obtaining blank page

EDIT

Was added the line:

require_once("OtherFunctions.php"); 

And was replaced the line:

require_once("SubClass.php");

by the line:

use \Pack\sp\SubClass;

in LeadClass.php File.

But, I'm obtaining blank page

1 Answer 1

1

You need to add the next line

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

Also I think you should put the functios of the OtherFunctions file into a new class link

namespace Pack\sp;

class OtherFunctions{
  // your current code goes here
}

After that you need to extend the SubClass whit the OtherFunctios class

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

EDIT I just tried your code and I can make the LeasClass to work as follow

<?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');

?>

Have you already initialize the class?

Sign up to request clarification or add additional context in comments.

2 Comments

I want to use OtherFunctions.php different to Class... Because is a Script of only functions without related to class.
Sorry it was lo late yesterday, I just edited my answer, hope is useful