Jump to content

Any idea why namespace aliasing won't work?


Andy-H

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 ??

Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

 

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.