I wanted to use code like this in my Zend Framework project:
public function indexAction() {
$user = new User();
$user->name = "Guest";
$user->save();
}
The main thing that is important for me is that the class is called just User and not App_Model_User but how could I manage that?
I thought I could add the path to the file User.php in the model-folder to the include_path:
<?php
class User {
/* Some code */
}
But how could I config the autoloader to load that file/class?
Currently I'm using the autoloader with the appnamespace ("App") to load classes called App_Model_User which works but the class naming is not the right I think. It should be cleaner and clearer.
I use set_include_path as follows:
$root = dirname(dirname(__FILE__));
set_include_path(
$root . '/application' . PATH_SEPARATOR
. $root . '/library' . PATH_SEPARATOR
. $root . '/application/models' . PATH_SEPARATOR
. $root . get_include_path()
);
Then the autoloader picks up any models (with any name) which I put in /application/models. The above is in my only public facing script (index.php). This is how I set up the autoloader:
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
The second line will ensure that your models do not have to be explicitly included. See:
http://framework.zend.com/manual/en/zend.loader.autoloader.html
What is your version of PHP? You can use namespaces.
Related
I encounterd a little problem with my classes : they simply do not load through my autoloader.
I get this message error :
Warning:
require(C:\wamp64\www\blog\appAutoloader.php/Table/PostsTable.php):
failed to open stream: No such file or directory in
C:\wamp64\www\blog\app\Autoloader.php on line 23
Fatal error: require(): Failed opening required
'C:\wamp64\www\blog\appAutoloader.php/Table/PostsTable.php'
(include_path='.;C:\php\pear') in
C:\wamp64\www\blog\app\Autoloader.php on line 23
Factory class :
use Core\config;
use Core\Database\MysqlDatabase;
class App {
public $title = "My super site";
private $db_instance;
private static $_instance;
public static function getInstance()
{
if (is_null(self::$_instance))
{
self::$_instance = new App();
}
return self::$_instance;
}
public static function load()
{
session_start();
require ROOT . '/app/Autoloader.php';
App\Autoloader::register();
require ROOT .'/core/Autoloader.php';
Core\Autoloader::register();
}
public function getTable($name)
{
$class_name = '\\App\\Table\\' . ucfirst($name) .'Table';
return new $class_name($this->getDb());
}
public function getDb()
{
$config = Config::getInstance(ROOT . '/config/config.php');
if (is_null($this->db_instance)) {
$this->db_instance = new MysqlDatabase($config->get('db_name'), $config->get('db_user'), $config->get('db_pass'), $config->get('db_host'));
}
return $this->db_instance;
}
}
Namespace App autoloader class :
<?php
namespace App;
class Autoloader {
static function register()
{
spl_autoload_register(array(__CLASS__, 'autoload')); // __CLASS__ load the current class
}
static function autoload($class)
{
if (strpos($class, __NAMESPACE__ .'\\') === 0) {
$class = str_replace(__NAMESPACE__ . '\\', '', $class); // _NAMESPACE_ load the current name_space
$class = str_replace('\\', '/', $class);
require __DIR__ . 'Autoloader.php/' . $class . '.php'; // __DIR__ = the parent folder. Here "app"
}
}
}
Namespace Core autoloader class :
<?php
namespace Core;
class Autoloader {
static function register()
{
spl_autoload_register(array(__CLASS__, 'autoload')); // __CLASS__ load the current class
}
static function autoload($class)
{
if (strpos($class, __NAMESPACE__ .'\\') === 0) {
$class = str_replace(__NAMESPACE__ . '\\', '', $class); // _NAMESPACE_ load the current name_space
$class = str_replace('\\', '/', $class);
require __DIR__ . 'Autoloader.php/' . $class . '.php'; // __DIR__ = the parent folder. Here "app"
}
}
}
Empty PostTable
namespace App\Table;
use Core\Table\Table;
class PostsTable extends Table
{
}
Index page :
define('ROOT', dirname(__DIR__));
require ROOT . '/app/App.php';
App::load();
$app = App::getInstance();
$posts = $app->getTable('Posts');
var_dump($posts->all());
How to make it works please?
AS I said in the comments check this path
require(C:\wamp64\www\blog\appAutoloader.php/Table/PostsTable.php)
Doesn't look right to me
require(C:\wamp64\www\blog\ [appAutoloader.php] /Table/PostsTable.php)
What's that bit doing there....
Also namespace of App is not app for the folder its App because this may work on Windows but you will find it does not work on Linux. Because Linux paths are case sensitive, and windows are not.
Further this makes little to no sense
require __DIR__ . 'Autoloader.php/' . $class . '.php'; // __DIR__ = the parent folder. Here "app"
Require 2 files? Paths don't work that way, not that I am aware of at least.
On top of that your implementation ignores the _ Typically underlines will be part of the class name but are replaced by directory, this allows a shorter namespace. So for example instead of having a namespace like this
Namespace \APP\Table;
class PostsTable ..
You could have a class in the same place Like so
Namespace \APP;
class Table_PostsTable ..
With a shorter namespace but still located in the App/Table/PostsTable.php file. However, that's just how I read the spec for PSR autoloaders.
PRO TIP
Take this path C:\wamp64\www\blog\appAutoloader.php/Table/PostsTable.php open the file browser on you desktop and see if it pulls up the file by pasting it into the navigation bar. It wont, but you can be sure your path is wrong by eliminating the code.
I don't know if I'm missing something but using namespaces seems to break my application. It won't find the parent class because it includes the namespace in the class name when the autoload register gets called
spl_autoload_register(function($classname){
if (preg_match('/[a-zA-Z]+Controller$/', $classname)) {
echo __DIR__ . '/controllers/' . $classname.".php" . "<br />";
require __DIR__ . '/controllers/' . $classname.".php";
}
});
//echo produces:
/var/www/web/controllers/DefaultController.php
/var/www/web/controllers/Project\Controllers\BaseController.php
Project\Controllers is the namespace used in both default and base controller.
Default extends base controller.
Why is spl autoload doing this?
Structure:
web/controllers:
BaseController.php
DefaultController.php
BaseController:
namespace Project\Controllers;
class BaseController
{
private $config;
public function __construct($config)
{
$this->config = $config;
}
}
DefaultController:
<?php
namespace Project\Controllers;
class DefaultController extends BaseController
{
}
The main problems seems to be that your autoloader is not aware of the root namespace which it belongs to. So really your autoloader should be made aware of or be part of the namespace so in this case Project.
So the first thing your autoload needs to do is remove the known root namespace (unless of course it is used as part of the file structure which in this case it isn't)
<?php namespace Project;
// Within the namespace
$classname = ltrim(str_replace(__NAMESPACE__ . '\\', '', $classname), '\\');
// Outside of the namespace
$classname = ltrim(str_replace('Project\\', '', $classname), '\\');
Now for the class name and the actual file location.
// find the last backslash
if($last = strripos($classname, '\\')) {
$namespace = substr($classname, 0, $last); // Controllers
$classname = substr($classname, $last+1); // DefaultController
}
Now you can built the actual file location to the PHP class file
$filepath = '/var/www/'; // Root
if(isset($namespace)) {
// Add the controllers bit onto the root
$filepath .= str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
// add the class name (DefaultController) to the file path
$filepath .= str_replace('\\', DIRECTORY_SEPARATOR, $classname) . '.php';
This should result in with the following filepaths which can be checked if exists then included if they do.
/var/www/Controllers/BaseController.php
/var/www/Controllers/DefaultController.php
I know it's common practice to autoload your controllers when using an MVC framework. I have made my own mini-framework where controllers are autoloaded fine.
Are there any security/bad issues with having the same autoload function load the models too?
I.e.
function __autoload($className) { // Autoload both controllers and models.
if(stristr($className, 'Model'))
{
if (is_readable(Ms . $className . '.php')) {
include Ms . $className . '.php';
}
} else {
if (is_readable(Cs . $className . '.php')) {
include Cs . $className . '.php';
}
}
}
You could use namespaces and spl_autoload_register() in order to get such an autoloader. There's no specific security issues regarding a multi autoloader (an autoloader for multi classes of classes) rather than a controller-only autoloader.
I usually works with namespaces like:
$home = new controller\home;
$home->actionIndex();
$users = new model\users;
$post = new view\post;
from there it's easy to replace a \ in the class name with a / to get the specific paths for the file (obviously doing the needed security checking as always).
I have created a config file that looks like:
$conf['db_hostname'] = "localhost";
$conf['db_username'] = "username";
$conf['db_password'] = "password";
$conf['db_name'] = "sample";
$conf['db_type'] = "mysql";
$conf['db_prefix'] = "exp";
and saved it as config.php.
Similarly the autoloader looks like
class autoloader {
public static $loader;
public static function init()
{
if(self::$loader == NULL) {
self::$loader = new self();
}
return self::$loader;
}
public function __construct()
{
spl_autoload_register(array(this, 'library'));
spl_autoload_register(array(this, 'controller'));
spl_autoload_register(array(this, 'helper'));
spl_autoload_register(array($this, 'model'));
}
public function library($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/lib');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function controller($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/controller');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function helper($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/helper');
spl_autoload_extensions('.php');
spl_autoload($class);
}
public function model($class)
{
set_include_path(get_include_path() . PATH_SEPARATOR . '/model');
spl_autoload_extensions('.php');
spl_autoload($class);
}
}
Where should I place both these files? In Root folder?
How these $configs will be available across the application?
How whould I use them in index.php? Should I create for $config array also a class?
How may I deny direct access to config.php file?
You must include your config file. I would recommend using require_once. Add this code to any files that will need the config variables. The best way to do this is to use a controller file, usually an index.php. That way you only need to add the require_once to a single file.
require_once("./config.php");
Don't worry about people viewing your database password, all php variables are purely server-side, and no php code or variables can be viewed by a client unless explicitly echoed out.
I am trying to autoload my PHP 5.3 namespaced classes eg.
/JM
Auth.php
User.php
Db/Entity.php
/ ...
I did
namespace KM;
class Autoloader {
public static function registerAutolaod() {
ini_set('include_path', dirname(__FILE__) . PATH_SEPARATOR . ini_get('include_path'));
spl_autoload_register(function ($classname) {
$file = preg_replace('/\\\/', DIRECTORY_SEPARATOR, $classname) . '.php';
echo $file . '<br />';
include ($file);
});
}
}
Problem is sometimes I get classname as User sometimes KM\User how can I fix this?
The $classname you receive in your callback function will be assumed to be local. That's because you have defined your __autoloader within a namespace.
To always get the absolute / fully qualified namespace and class name, declare your autoloader in the global scope. http://www.php.net/manual/en/language.oop5.autoload.php#87985