Jump to content

Recommended Posts

I keep finding examples using inheritance and interfaces that are in the same file.

Example:

 

abstract class abFoo{

  public $name;

}

class foo extends abFoo{

  //do stuff

}

 

My question deals with the fact that my abstract classes are not in the same directory as my other classes.  There I have found myself having to use include_once in my foo class to include abFoo.  This does make sense, in Java you have to include files you want to extend or implement.  The thing i dont like about this is that i have to use the full absolute file path to include the abstract file in the sub class file.  so ive done the following and would like to hear some thoughts.

 

Currently I have an object factory that creates all my objects for me.  the object factory handles all my object creation and takes care of dependency injections too.  in the constructor method of the object factory i am setting two values in the $GLOBAL array, one for path to my abstracts directory and one for my interfaces directory.  then in my sub classes above the class definition all i have to say is

 

include_once($GLOBALS["abstractPath"]."someAbstractFile.php");

include_once($GLOBALS["interfacePath"]."someInterfaceFile.php");

 

Is there a better way to do this?  I have been reading over the __autoloader but I still dont quite understand how that is working.  Is there a more efficient way of doing this then how what I am doing now.  I realize my objects are now tied to looking at the $GLOBAL array which i dont necessarily like either. 

 

Ideally I would think that I could do the include_once inside my object factory but that doesnt seem to work. 

 

thanks in advance for any help.

 

Link to comment
https://forums.phpfreaks.com/topic/101395-question-on-inheritance-and-interfaces/
Share on other sites

The way I do it is to have a couple of naming conventions and folder structure conventions.

 

If we say that all classes are placed in library, then the class PHPFreaks_Auth_Plugin_SMF would be in library/PHPFreaks/Auth/Plugin/SMF.php. That makes the __autoload() function very simple:

 

<?php
function __autoload($className)
{
    require_once 'library' . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
}
?>

now it seems that if i add the include_once to the constructor of my object factory it works fine.  not sure if this is a good solution to be in the constructor though b/c i will always be loading up the abstract and interface classes even though i may not need them.  i think ill move it out of the constructor and down to where the objects are created.

 

i noticed  you used require_once instead of include_once, is this a personal preference or is there some performance differences between the two?

 

 

i noticed  you used require_once instead of include_once, is this a personal preference or is there some performance differences between the two?

 

The difference is the level of error that will occur if the file doesn't exist. include() and include_once() gives a warning whereas require() and require_once() gives a fatal error. A warning allows the script to continue, but a fatal error would stop the execution.

so which class gets the __autoloader function?

 

Every class which is not yet defined would have its name passed to the autoloader.

 

also how would it know to include the correct abstract and interface classes? 

 

In my example it would because all classes and interfaces are laid out in a specific directory pattern based on the name of the class, therefore it "knows" where a specific class or interface should be located on the disk.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.