Jump to content
Stef's Coding Community
Sign in to follow this  
sb17joker

Question about autoloading classes & how to deal with them if they don't exist

Recommended Posts

This message is targeted at john lebensold, but any answers are welcome :)

 

I have an application I'm writing, & would like to give the option for customization down the road, but also have it upgradable to version 2.0.. 3.0.. 4.0 in the future without having to edit the main files at the time of upgrade to keep customizations. I have created on one, I have a Cms class, & have made a "customCms extends Cms Class" file, but don't necessarily want to write a blank file for every class so that down the road I can possibly customize it (mainly because when I deploy the new version I don't want to chance overwriting the end programers customizations if I don'[t delete the custom file before packaging). I have this code, but it doesn't do that, it will error if the file is not found. The autoloader works, but the "$Cms = new Cms" doesn't, it errors. Is there a way to load a class only if it exists?

 

the code I'm using is:

 

function __autoload($className)

{

if(file_exists($filename = str_replace('\\', '/' , $className) . '.php')){

require_once $filename;

}else{

return FALSE;

}

}

 

& this I believe is working, if it doesn't find the file it returns false.

 

//create cms object

if(!$this->Cms = new \enterprize\controllers\customCms()){

$this->Cms = new \enterprize\controllers\Cms();

}

 

but this part is not working, even tho I have the if(), it's erroring while it's trying to create $Cms.

Share this post


Link to post
Share on other sites

I decided to go this route for the time being, but still looking for better solutions.

 

if(file_exists(APP_PATH."\controllers\CmsCustom.php")){

$this->Cms = new \enterprize\controllers\CmsCustom();

}else{

$this->Cms = new \enterprize\controllers\Cms();

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...