Andy-H Posted May 24, 2012 Share Posted May 24, 2012 PHP 5.3.2-1 This works fine: define('ENV', 'DEVELOPMENT'); include '../com/config/bootstrap.php'; try { /** * SET NAMESPACE ALIASES */ //use \com\controllers as Controller; //use \com\models as Model; /** * ESTABLISH CONNECTION TO DATABASE */ $dbh = new PDO('mysql:dbname='. DB .';host='. DB_HOST, DB_USER, DB_PASS); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'UTF8'"); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); if ( isset($_GET['path']) ) { $path = explode('/', $_GET['path']); $Controller = '\\com\\controllers\\'. ucfirst($path[0]); $Controller = new $Controller; } } catch ( PDOException $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => 'Could not connect to database')); //$e->getMessage(); } catch ( ClassNotFoundException $e ) { echo $e->getMessage(); } catch ( Exception $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => $e->getMessage())); } but when I try define('ENV', 'DEVELOPMENT'); include '../com/config/bootstrap.php'; try { /** * SET NAMESPACE ALIASES */ use \com\controllers as Controller; //use \com\models as Model; /** * ESTABLISH CONNECTION TO DATABASE */ $dbh = new PDO('mysql:dbname='. DB .';host='. DB_HOST, DB_USER, DB_PASS); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'UTF8'"); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); if ( isset($_GET['path']) ) { $path = explode('/', $_GET['path']); $Controller = 'Controller\\'. ucfirst($path[0]); $Controller = new $Controller; } } catch ( PDOException $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => 'Could not connect to database')); //$e->getMessage(); } catch ( ClassNotFoundException $e ) { echo $e->getMessage(); } catch ( Exception $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => $e->getMessage())); } I get server error, access denied to page, any ideas why? Thanks // EDIT, forgot to uncomment use com\controllers, as soon as I uncomment this, I get the error, regardless of whether I use the aliased namespace or not. Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/ Share on other sites More sharing options...
trq Posted May 24, 2012 Share Posted May 24, 2012 What is the exact error? Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348243 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 I'm not sure, my works server is set up weird, I have display_errors on and error_reporting E_ALL | E_STRICT but I just get a 500 server error. I don't know why it does this? Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348246 Share on other sites More sharing options...
trq Posted May 24, 2012 Share Posted May 24, 2012 Check your server log. Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348247 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 Just checked php.ini and log errors is off Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348251 Share on other sites More sharing options...
trq Posted May 24, 2012 Share Posted May 24, 2012 Still, so turn it on. Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348253 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 Checked apache log, unexpected T_USE in index.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348254 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 Was because use was inside of the curly braces http://www.php.net/manual/fa/language.namespaces.importing.php#98908 <?php define('ENV', 'DEVELOPMENT'); include '../com/config/bootstrap.php'; use com\controllers as Controller; use \com\models as Model; try { /** * SET NAMESPACE ALIASES */ /** * ESTABLISH CONNECTION TO DATABASE */ $dbh = new PDO('mysql:dbname='. DB .';host='. DB_HOST, DB_USER, DB_PASS); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'UTF8'"); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); if ( isset($_GET['path']) ) { $path = explode('/', $_GET['path']); $Controller = 'Controller\\'. ucfirst($path[0]); $Controller = new $Controller; } } catch ( PDOException $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => 'Could not connect to database')); //$e->getMessage(); } catch ( ClassNotFoundException $e ) { echo $e->getMessage(); } catch ( Exception $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => $e->getMessage())); } ?> Works, however, it still doesn't alias it and tries to resolve to Contoller/User.class.php ?? Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348257 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 Tried : <?php define('ENV', 'DEVELOPMENT'); include '../com/config/bootstrap.php'; // Namespace aliases use \com\controllers as Controller; use \com\models as Model; try { /** * SET NAMESPACE ALIASES */ /** * ESTABLISH CONNECTION TO DATABASE */ $dbh = new PDO('mysql:dbname='. DB .';host='. DB_HOST, DB_USER, DB_PASS); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'UTF8'"); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); if ( isset($_GET['path']) ) { $path = explode('/', $_GET['path']); $Controller = ucfirst($path[0]); $Controller = new Controller\$Controller; } } catch ( PDOException $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => 'Could not connect to database')); //$e->getMessage(); } catch ( ClassNotFoundException $e ) { echo $e->getMessage(); } catch ( Exception $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => $e->getMessage())); } ?> Getting unexpected T_NAMESPACE_SEPARATOR expected T_STRING on line 22, thinking maybe this behaviour isn't supported, although I'm sure it should be? Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348261 Share on other sites More sharing options...
trq Posted May 24, 2012 Share Posted May 24, 2012 Not like that it's not. Try; $Controller = 'Controller\\' . ucfirst($path[0]); $Controller = new $Controller; Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348263 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 I've tried that, it loads but just doesn't resolve Controller to \com\controllers //EDIT, I also tried double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348264 Share on other sites More sharing options...
trq Posted May 24, 2012 Share Posted May 24, 2012 Have you actually included the Controller class somehow? Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348266 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 It gets autoloaded /** * AUTOLOAD CLASSES */ include 'ClassNotFoundException.class.php'; function __autoload($class) { $class = explode('\\', $class); $classname = array_pop($class); $file = APPLICATION_PATH . implode(DIRECTORY_SEPARATOR, $class) . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $classname) .'.class.php'; if ( !file_exists($file) ) throw new ClassNotFoundException($file); include $file; } Catching ClassNotFoundException and echoing getMessage outputs : /var/www/api.domain.com/Controller/User.class.php Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348269 Share on other sites More sharing options...
trq Posted May 24, 2012 Share Posted May 24, 2012 Catching ClassNotFoundException and echoing getMessage outputs : /var/www/api.domain.com/Controller/User.class.php Are you saying it isn't being autoloaded then? Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348271 Share on other sites More sharing options...
kicken Posted May 24, 2012 Share Posted May 24, 2012 $Controller = 'Controller\\' . ucfirst($path[0]); $Controller = new $Controller; Importing is performed at compile-time, and so does not affect dynamic class, function or constant names. Doesn't work with dynamic names. You'll probably just have to include the full namespace in the string rather than try and do a use statement. Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348272 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 $Controller = 'Controller\\' . ucfirst($path[0]); $Controller = new $Controller; Importing is performed at compile-time, and so does not affect dynamic class, function or constant names. Doesn't work with dynamic names. You'll probably just have to include the full namespace in the string rather than try and do a use statement. That's an oversight surely? Is this expected behaviour? I would have thought if I double quoted the namespace alias it would interpolate like a variable does :/ Is there a better way than this to load controllers anyway?? Also, I am unsure as to how I would inject objects into methods doing things this way, as my controller can't take arguments, is it common for controllers to contain references to super global arrays or have I overlooked something? Also, how would you recommend injecting my PDO object into dependant classes? Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348274 Share on other sites More sharing options...
trq Posted May 24, 2012 Share Posted May 24, 2012 Is there a better way than this to load controllers anyway?? Also, I am unsure as to how I would inject objects into methods doing things this way, as my controller can't take arguments, is it common for controllers to contain references to super global arrays or have I overlooked something? Also, how would you recommend injecting my PDO object into dependant classes? I'm not sure any of these questions can be answered in a simple forum post. There are much more robust ways of loading controllers. And any dependencies they need should be loaded in via the __construct. As for loading PDO. That is a prime candidate for dependency injection. Google it. There is a link to my framework in my signature, it's still in a pre-alpha stage, but it's well worth a look at. Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348445 Share on other sites More sharing options...
Andy-H Posted May 24, 2012 Author Share Posted May 24, 2012 The idea was to inject PDO into my controllers, but then I'm creating a database connection for every request, what if I don't need one? I get all the concepts of OOP, and know a few design patterns, it's just putting them into use that I always struggle with, maybe I'm concentrating too much on creating the perfect application rather than "an application" :/ I've read a lot about your proem framework, it looks impressive But we're running 5.3, is 5.4 stable now? Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348457 Share on other sites More sharing options...
trq Posted May 25, 2012 Share Posted May 25, 2012 5.4 is at 5.4.3 now. Stable. PDO does not belong in your controllers. You should only be accessing the database via Models. Quote Link to comment https://forums.phpfreaks.com/topic/263043-any-idea-why-namespace-aliasing-wont-work/#findComment-1348477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.