1

I have a problem with including classes. Here's a simple example to explain the problem:

Class no. 1 (path: /home/day/Class_A.php):

class Class_A {
  public function sayHi() {
    echo "Good day";
  }
}

Class no. 2 (path: /home/test/Class_B.php):

class Class_B {
  public function greeting() {
    if(class_exists("Class_A")!=true)
      include "../day/Class_A.php";
    $test = new Class_A();
    $test->sayHi();
  }
}

PHP file (path: /home/php.php)

if(class_exists("Class_B")!=true)
  include "test/Class_B.php";
  $g = new Class_B;
  $g->greeting();

The problem is when php.php includes Class_B and Class_B includes Class_A, Class_B fails to open Class_B because the path of the object of the class is now the same as the php.php file.

My question is: Is there a good and simple way to get around this?

1
  • Store classes under a directory that you can add to the include path. Use require_once and make the path (in the require_once construct) relative to the path added to the include path for classes. Commented Jul 30, 2012 at 20:53

3 Answers 3

6

Try changing:

include "../day/Class_A.php";

to

include dirname(__FILE__) . '/../day/Class_A.php';

This way, your include will be relative to the file that is doing the include (Class_B).

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

5 Comments

Thanks, but I actually want the opposite, that is: it will always be relative to the class.
Is Class_B always going to be in /dir/test/ and Class_A will always be in /dir/day/ ?
Then if you use the method I gave it should be fine. dirname(__FILE__) returns the path from the file that code is executed in, which is Class_B.
Yes it did. I missed the "/" before ".." earlier. This is what I was looking for. Thanks man.
Ah good, sorry about that yeah I forgot it originally and then edited the post to add the / but you were quick and tried it before the edit :)
0

Looks like greeting is not a static function so you can't do:

Class_B::greeting();

You'd have to get a class B object and called greeting on it or add static to greetings declarations.

Second, why not uuse require_once? In php.php

require_once('test/ClassB.php');

And in ClassB.php:

require_once('../day/ClassA.php');

2 Comments

I read that require_once is not a good idea because it looses memory. Don't remember the reason. I'll fix Class_B..
Not that it loses memory but it adds logic since it has to check if a file has already been loaded, but I think the benefits and simplicity outweight these small performance issues. Take a look at this: stackoverflow.com/questions/186338/…
0

Use __autoload which is a magic function, that you define, that enables PHP to let you know when it doesn't have a class loaded, but that class needs to be loaded.

If you define the __autoload function like so,
function __autoload ($classname)
{
    require('/path/to/my/classes/'.$classname.'.php');
}
you no longer need to add
require_once('/path/to/my/classes/MyClass.php');
into your files, because the first time that PHP encounters
$mine = new MyClass();
or
MyClass::staticMethodCall();
it will automatically call the __autoload function that you defined earlier.
__autoload('MyClass');

Resource: http://blog.samshull.com/2010/02/why-you-should-use-autoload-function-in.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.