How to prevent errors during include if file not exist (without @)

Go To StackoverFlow.com

1

Hi guys could you help me with next.

I have next code

index.php

set_include_path(get_include_path()
            .PATH_SEPARATOR.'application/controllers'
            .PATH_SEPARATOR.'application/models'
            .PATH_SEPARATOR.'application/views');
spl_autoload_register();
$front = new Controller;
$front->route();

Controller.php

public function route(){
    if(class_exists($this->getController())){...}......

I have one way. Insted of using spl_autoload_register(); I can write

function __autoload($classname) {
 @include_once $classname.'.php';
}

But according php documentation, I want to use spl_autoload_register... If you need full code, I'll be glad to demonstrate it.

Thank you!

2012-04-03 19:54
by Vitalii Plodistov
Please ask a question - Carsten 2012-04-03 19:57
Question in header - How to prevent errors during include if file not exis - Vitalii Plodistov 2012-04-03 20:01
Your task is quite impossible. If you are autoloading a class that doesn't exist, just preventing the error from trying to include that file won't solve your problem, because you're trying to use a class that PHP doesn't know about - Carsten 2012-04-03 20:07
You're right. Probably, my answer is how to bind getincludepath and file_exists.... - Vitalii Plodistov 2012-04-03 20:22
see my modified answe - Matthew Blancarte 2012-04-03 20:34
I made it next way:

$arr = explode(PATH_SEPARATOR,get_include_path());
foreach ($arr as $val){

if(fileexists($val.'/'.$classname.'.php')){ includeonce $classname.'.php'; break; } }

Thank you everyone - Vitalii Plodistov 2012-04-03 20:47



1

You could just use file_exists, I suppose...

http://php.net/manual/en/function.file-exists.php

if( file_exists( 'path/to/' . $classname / '.php' ) )
{
  include_once( $classname.'.php' );
}

Why are you autoloading classes that don't exist?

You could also try this:

if( !include_once( $classname.'.php' ) )
{
  //throw error, or do something... or nothing
  throw new Exception( "Autoload for $classname failed. Bad path." );
}
2012-04-03 20:05
by Matthew Blancarte
I don't know path! It in getincludepath(). I've tried your way before.. - Vitalii Plodistov 2012-04-03 20:09
Did you see my question? Why are you loading classes that don't exist? That's really the way to stop errors from being thrown lol. Also, why don't you know the path - Matthew Blancarte 2012-04-03 20:11
I'm creating my own framework. I'm making routing throught url. For example: http://localhost/admin/index It means, automatically will open function index of class "admin". I made it succesfully. But now, I'm working with protection. For example if try to go to http://localhost/anything/index, it will generate error, because I don't have class "anything - Vitalii Plodistov 2012-04-03 20:17
Hmmm... see modified answer.. - Matthew Blancarte 2012-04-03 20:31
good trial, but it still generate error. Here is should be used try catche operato - Vitalii Plodistov 2012-04-03 20:45
Yes that was just a psuedo code example. You'd want to try/catch if throwing exceptions. So your concern is that if you kept all of your class files in one directory, that you'll have some kind of security breach? Like someone will execute a class file without permission, or in the wrong part of your framework - Matthew Blancarte 2012-04-03 20:50
I made it pretty cool! The classes that shouldn't be executed aren't implementet empty interface "IController". And after I'm checking implementation by next code - if(!$rc->implementsInterface('IController')){header("HTTP/1.0 404 Not Found"); - Vitalii Plodistov 2012-04-03 21:02


1

Here is the code that I use (I hope it will give you an idea):

<?php
Class ClassAutoloader {

  function __construct() {
    spl_autoload_register(array($this, 'loader'));
  }

  private function loader($className) {

    $classPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'classes'.DIRECTORY_SEPARATOR;
    if(file_exists( $classPath.strtolower($className).'.php' )) {
      include $classPath.strtolower($className).'.php' ;
    } else if(file_exists( $classPath.$className.DIRECTORY_SEPARATOR.strtolower($className).'.php' )) {
      include $classPath.$className.DIRECTORY_SEPARATOR.strtolower($className).'.php';
    }
  }
}
2012-04-03 20:13
by Stanislav


0

You could turn off errors with

 <?PHP 
 error_reporting(E_ALL & ~E_WARNING)
 ?>
2012-04-03 20:05
by Nathan
not good idea too, I want to somehow make checking if fileexists, but I can't use that function because path is in getinclude_path( - Vitalii Plodistov 2012-04-03 20:08
Ads