2

I am including one PHP script into another using PHP's require_once() method. This script contains a class, TemplateAdmin, which instantiates itself right after the script, like this:

class TemplateAdmin {
// Class body...
}

$templateAdmin = new TemplateAdmin();

This was working fine for a while. However, I have adopted a new importing technique to include classes and packages. I have tested this new technique, and it works! However, for some strange reason, none of the methods in any of the classes I import are there when I need them. However, it seems as though the instance variables are still there.

For example, when a class with this absolute path is called:

require_once("C:\wamp\www\wave_audio\system\server\templates\TemplateAdmin.php");

... I get this error in the call stack:

Fatal error: Call to undefined method stdClass::top() in C:\wamp\www\wave_audio\cms\index.php on line 189

This error is referring to my use of the top() method inside of the TemplateAdmin class.

Does any one have any idea as to why this is happening??? If this helps, I have been using require_once() all along, I am running PHP 5.3.5 on a Windows XP Media Center machine.

Thank you for your time!

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • 2
    It would be best if you could show the full relevant source code (where do you do the require, where do you call top()).... – Pekka Jul 11 '11 at 22:26
  • Can you reduce your code down to a minimal test case, and then post it here? – Oliver Charlesworth Jul 11 '11 at 22:27
  • It is too long to include the entire script, but I can assure you that the script *is* imported before the method is called.It works, as I have tried echoing some text inside of the included file, which displays on my screen before I receive the error. I'll try to boil this down to a simplified version... :) – Oliver Spryn Jul 11 '11 at 22:28
  • Post the code around ` C:\wamp\www\wave_audio\cms\index.php on line 189` – Glass Robot Jul 11 '11 at 22:29
  • Here is a *really* stripped-down version of the code. It exemplifies the point of my script: http://pastie.org/private/rbpdvy7tdom61ts9ouf3w Let me know if it is too hard to follow... – Oliver Spryn Jul 11 '11 at 22:39
  • Do You have code like `class something{ /*...*/ require_once('something_else'); /*...*/ }` ? – Michas Jul 11 '11 at 22:41
  • No, the TemplateAdmin class does not require any additional scripts. That is the job of of the `import()` function. – Oliver Spryn Jul 11 '11 at 22:43
  • 1
    Is there `require_once()` in `import()` function? – Michas Jul 11 '11 at 22:45
  • yes, there is. Here is a simplified version of the code http://pastie.org/private/rbpdvy7tdom61ts9ouf3w. – Oliver Spryn Jul 11 '11 at 22:50
  • 1
    I think that might be your problem `$templateAdmin` is trapped inside the `import` function. – Glass Robot Jul 11 '11 at 22:54
  • Hmm... so you are saying that it could be a scope issue? – Oliver Spryn Jul 11 '11 at 22:57
  • It *is* a scope issue.... Trying `$templateAdmin->top();` inside of the `import()` function right after it is imported will actually run the method!!! Any idea how I can get it out of this trap? – Oliver Spryn Jul 11 '11 at 23:00

2 Answers2

1

Assuming you dont want to use globals here is one way that only requires a few changes.

TemplateAdmin.php:

class TemplateAdmin {
// Class body...
}

return new TemplateAdmin();

Return include once in import:

function import($classes) {
//Convert ECMAScript style directory structures to Unix style
  $address = str_replace(".", "/", $classes);
  $address = INSTALL_ROOT . "system/server/" . $address . ".php";

  if (file_exists($address) && is_file($address)) {
    return require_once($address);
  } else {
    die(""" . $classes . "" does not link to an existing class");
  }
}

Assign the variable:

$adminTemplate = import('templates.TemplateAdmin');
Glass Robot
  • 2,438
  • 19
  • 11
  • Hmm... thanks Glass Robot! I'll have to look into this. Ideally, I'd like to use use `import('templates.TemplateAdmin')` as a quick alternative to `require_once(/* The full path */)`, without assigning its contents to a variable, and having it function the exact same way. Do you know that that kind of thing is possible? – Oliver Spryn Jul 11 '11 at 23:24
0

I have a feeling your php error message is accurate. I know on your stripped down version, you pieced it together how you're sure it's setup but it's obviously not a direct copy/paste since it's like:

class TemplateAdmin {
  public function top() {
  //The "top" method...
  }
}

So, the error message says that the method "top" is not defined. If it were not including your file properly, it would tell you that the class you instantiated doesn't exist. Either that method does not exist in the class you think it is, or the method has been unset somewhere in that object instance. Trust your error message.

Jage
  • 7,990
  • 3
  • 32
  • 31