Factory pattern using class_exists on CakePHP - php

I have the following code (trying to simulate a factory pattern):
CoinConnectorFactory.php
public static function build($connector) {
$connector = "CoinConnector" . ucwords($connector);
if (class_exists($connector)) {
return new $connector();
} else {
throw new InvalidConnectorType($connector);
}
}
And the following folder structure:
/app
/Plugin
/CoinConnector
/Lib
CoinConnectorFactory.php
CoinConnectorGogulski.php
So the problem is, I pass to the build method as a $connector variable this value gogulski but when get into class_exists like this:
class_exists('CoinConnectorGogulski')
Never find the class (that have the same name that the file) and always throw the exception.
Only if I add this line before check if the class exist CakePHP is able to find the class
include_once APP . 'Plugin' . DS . 'CoinConnector' . DS . 'Lib' . DS . $connector . '.php';

That is how I fix my problem at the end:
public static function build($connector) {
$connector = "CoinConnector" . ucwords($connector);
App::uses($connector, 'CoinConnector.Lib');
if (class_exists($connector)) {
return new $connector();
} else {
throw new InvalidConnectorType($connector);
}
}

Related

Class not Found in PHP PDO FetchAll FETCH_CLASS [duplicate]

I'm working on a project whereby I have the following file structure:
index.php
|---lib
|--|lib|type|class_name.php
|--|lib|size|example_class.php
I'd like to auto load the classes, class_name and example_class (named the same as the PHP classes), so that in index.php the classes would already be instantiated so I could do:
$class_name->getPrivateParam('name');
I've had a look on the net but can't quite find the right answer - can anyone help me out?
EDIT
Thanks for the replies. Let me expand on my scenario. I'm trying to write a WordPress plugin that can be dropped into a project and additional functionality added by dropping a class into a folder 'functionality' for example, inside the plugin. There will never be 1000 classes, at a push maybe 10?
I could write a method to iterate through the folder structure of the 'lib' folder, including every class then assigning it to a variable (of the class name), but didn't think that was a very efficient way to do it but it perhaps seems that's the best way to achieve what I need?
Please, if you need to autoload classes - use the namespaces and class names conventions with SPL autoload, it will save your time for refactoring.
And of course, you will need to instantiate every class as an object.
Thank you.
Like in this thread:
PHP Autoloading in Namespaces
But if you want a complex workaround, please take a look at Symfony's autoload class:
https://github.com/symfony/ClassLoader/blob/master/ClassLoader.php
Or like this (I did it in one of my projects):
<?
spl_autoload_register(function($className)
{
$namespace=str_replace("\\","/",__NAMESPACE__);
$className=str_replace("\\","/",$className);
$class=CORE_PATH."/classes/".(empty($namespace)?"":$namespace."/")."{$className}.class.php";
include_once($class);
});
?>
and then you can instantiate your class like this:
<?
$example=new NS1\NS2\ExampleClass($exampleConstructParam);
?>
and this is your class (found in /NS1/NS2/ExampleClass.class.php):
<?
namespace NS1\NS2
{
class Symbols extends \DB\Table
{
public function __construct($param)
{
echo "hello!";
}
}
}
?>
If you have an access to the command line, you can try it with composer in the classMap section with something like this:
{
"autoload": {
"classmap": ["yourpath/", "anotherpath/"]
}
}
then you have a wordpress plugin to enable composer in the wordpress cli : http://wordpress.org/plugins/composer/
function __autoload($class_name) {
$class_name = strtolower($class_name);
$path = "{$class_name}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The file {$class_name}.php could not be found!");
}
}
UPDATE:
__autoload() is deprecated as of PHP 7.2
http://php.net/manual/de/function.spl-autoload-register.php
spl_autoload_register(function ($class) {
#require_once('lib/type/' . $class . '.php');
#require_once('lib/size/' . $class . '.php');
});
I have an example here that I use for autoloading and initiliazing.
Basically a better version of spl_autoload_register since it only tries to require the class file whenever you initializes the class.
Here it automatically gets every file inside your class folder, requires the files and initializes it. All you have to do, is name the class the same as the file.
index.php
<?php
require_once __DIR__ . '/app/autoload.php';
$loader = new Loader(false);
User::dump(['hello' => 'test']);
autoload.php
<?php
class Loader
{
public static $library;
protected static $classPath = __DIR__ . "/classes/";
protected static $interfacePath = __DIR__ . "/classes/interfaces/";
public function __construct($requireInterface = true)
{
if(!isset(static::$library)) {
// Get all files inside the class folder
foreach(array_map('basename', glob(static::$classPath . "*.php", GLOB_BRACE)) as $classExt) {
// Make sure the class is not already declared
if(!in_array($classExt, get_declared_classes())) {
// Get rid of php extension easily without pathinfo
$classNoExt = substr($classExt, 0, -4);
$file = static::$path . $classExt;
if($requireInterface) {
// Get interface file
$interface = static::$interfacePath . $classExt;
// Check if interface file exists
if(!file_exists($interface)) {
// Throw exception
die("Unable to load interface file: " . $interface);
}
// Require interface
require_once $interface;
//Check if interface is set
if(!interface_exists("Interface" . $classNoExt)) {
// Throw exception
die("Unable to find interface: " . $interface);
}
}
// Require class
require_once $file;
// Check if class file exists
if(class_exists($classNoExt)) {
// Set class // class.container.php
static::$library[$classNoExt] = new $classNoExt();
} else {
// Throw error
die("Unable to load class: " . $classNoExt);
}
}
}
}
}
/*public function get($class)
{
return (in_array($class, get_declared_classes()) ? static::$library[$class] : die("Class <b>{$class}</b> doesn't exist."));
}*/
}
You can easily manage with a bit of coding, to require classes in different folders too. Hopefully this can be of some use to you.
You can specify a namespaces-friendly autoloading using this autoloader.
<?php
spl_autoload_register(function($className) {
$file = __DIR__ . '\\' . $className . '.php';
$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
if (file_exists($file)) {
include $file;
}
});
Make sure that you specify the class file's location corretly.
Source
spl_autoload_register(function ($class_name) {
$iterator = new DirectoryIterator(dirname(__FILE__));
$files = $iterator->getPath()."/classes/".$class_name.".class.php";
if (file_exists($files)) {
include($files);
} else {
die("Warning:The file {$files}.class.php could not be found!");
}
});
do this in a file and called it anything like (mr_load.php)
this were u put all your classes
spl_autoload_register(function($class){
$path = '\Applicaton/classes/';
$extension = '.php';
$fileName = $path.$class.$extension;
include $_SERVER['DOCUMENT_ROOT'].$fileName;
})
;
then create another file and include mr_load.php; $load_class = new BusStop(); $load_class->method()

SPL_AUTOLOAD_REGISTER only registering

I'm using
function Autoload($ClassName) {
if (is_readable(CLASSES.'/' . strtolower($ClassName) . '.class')){
require(CLASSES.'/' . strtolower($ClassName) . '.class');
}
}
spl_autoload_register("Autoload");
$site = new site();
$theme = new theme();
for an autoload function of classes that I'll be using.
When I try to use $theme->function inside of my themes directory I'm getting
Undefined variable: theme in
I can fix this by placing $theme = new theme(); at the top of the php file calling it, but to my understanding the autoload was to get rid of the need to do that. Is there something wrong with the code I'm using to register the classes or am I misunderstanding the reason behind autoload?
My Old Method
function LoadClasses() {
foreach(glob(CLASSES.'/'.'*.class') as $classfile) {
require $classfile;
}
}

Spl_autoload_register() not working on server

I made a simple autoloader using spl_autoloader_register function, it works fine on my virtual server, but in the server I only got "Fatal Error: Class 'X' not found". Im running it on a mac with PHP 5.4, but it also works in windows/ubuntu with 5.3 version, which is the same as my physic server. I don't have SSH access to it. Here is my autoload code:
class Load
{
public static function autoload($class)
{
$class = strtolower($class);
$lib = $_SERVER['DOCUMENT_ROOT'] . BASENAME . "/libs/{$class}.php";
$model = $_SERVER['DOCUMENT_ROOT'] . BASENAME . "/models/{$class}.class.php";
$controller = $_SERVER['DOCUMENT_ROOT'] . BASENAME . "/controllers/{$class}.php";
if(is_readable($lib)){
require_once $lib;
}elseif (is_readable($model)) {
require_once $model;
}elseif (is_readable($controller)){
require_once $controller;
}
}
}
spl_autoload_register("Load::autoload");
I always used spl for local apps, but its the first time I'm trying it on the server.
Any advice for better practices will be helpful.
Thanks
A good practice can be to add your own include path. Then you can disclaim $_SERVER['DOCUMENT_ROOT'].
For example..
// Define path to library
define('MY_LIBRARY_PATH', realpath(dirname(__FILE__) . '/../insert_path_here_relativly'));
// Ensure library is on include_path
set_include_path(
get_include_path() . PATH_SEPARATOR . MY_LIBRARY_PATH
);
Then your autoloader..
class Load
{
public static function autoload($class)
{
$class = strtolower($class);
$lib = MY_LIBRARY_PATH . "/libs/{$class}.php";
$model = MY_LIBRARY_PATH . "/models/{$class}.class.php";
$controller = MY_LIBRARY_PATH . "/controllers/{$class}.php";
if(is_readable($lib)){
require_once $lib;
}elseif (is_readable($model)) {
require_once $model;
}elseif (is_readable($controller)){
require_once $controller;
}
}
}
spl_autoload_register("Load::autoload");

Best Way To Autoload Classes In PHP

I'm working on a project whereby I have the following file structure:
index.php
|---lib
|--|lib|type|class_name.php
|--|lib|size|example_class.php
I'd like to auto load the classes, class_name and example_class (named the same as the PHP classes), so that in index.php the classes would already be instantiated so I could do:
$class_name->getPrivateParam('name');
I've had a look on the net but can't quite find the right answer - can anyone help me out?
EDIT
Thanks for the replies. Let me expand on my scenario. I'm trying to write a WordPress plugin that can be dropped into a project and additional functionality added by dropping a class into a folder 'functionality' for example, inside the plugin. There will never be 1000 classes, at a push maybe 10?
I could write a method to iterate through the folder structure of the 'lib' folder, including every class then assigning it to a variable (of the class name), but didn't think that was a very efficient way to do it but it perhaps seems that's the best way to achieve what I need?
Please, if you need to autoload classes - use the namespaces and class names conventions with SPL autoload, it will save your time for refactoring.
And of course, you will need to instantiate every class as an object.
Thank you.
Like in this thread:
PHP Autoloading in Namespaces
But if you want a complex workaround, please take a look at Symfony's autoload class:
https://github.com/symfony/ClassLoader/blob/master/ClassLoader.php
Or like this (I did it in one of my projects):
<?
spl_autoload_register(function($className)
{
$namespace=str_replace("\\","/",__NAMESPACE__);
$className=str_replace("\\","/",$className);
$class=CORE_PATH."/classes/".(empty($namespace)?"":$namespace."/")."{$className}.class.php";
include_once($class);
});
?>
and then you can instantiate your class like this:
<?
$example=new NS1\NS2\ExampleClass($exampleConstructParam);
?>
and this is your class (found in /NS1/NS2/ExampleClass.class.php):
<?
namespace NS1\NS2
{
class Symbols extends \DB\Table
{
public function __construct($param)
{
echo "hello!";
}
}
}
?>
If you have an access to the command line, you can try it with composer in the classMap section with something like this:
{
"autoload": {
"classmap": ["yourpath/", "anotherpath/"]
}
}
then you have a wordpress plugin to enable composer in the wordpress cli : http://wordpress.org/plugins/composer/
function __autoload($class_name) {
$class_name = strtolower($class_name);
$path = "{$class_name}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The file {$class_name}.php could not be found!");
}
}
UPDATE:
__autoload() is deprecated as of PHP 7.2
http://php.net/manual/de/function.spl-autoload-register.php
spl_autoload_register(function ($class) {
#require_once('lib/type/' . $class . '.php');
#require_once('lib/size/' . $class . '.php');
});
I have an example here that I use for autoloading and initiliazing.
Basically a better version of spl_autoload_register since it only tries to require the class file whenever you initializes the class.
Here it automatically gets every file inside your class folder, requires the files and initializes it. All you have to do, is name the class the same as the file.
index.php
<?php
require_once __DIR__ . '/app/autoload.php';
$loader = new Loader(false);
User::dump(['hello' => 'test']);
autoload.php
<?php
class Loader
{
public static $library;
protected static $classPath = __DIR__ . "/classes/";
protected static $interfacePath = __DIR__ . "/classes/interfaces/";
public function __construct($requireInterface = true)
{
if(!isset(static::$library)) {
// Get all files inside the class folder
foreach(array_map('basename', glob(static::$classPath . "*.php", GLOB_BRACE)) as $classExt) {
// Make sure the class is not already declared
if(!in_array($classExt, get_declared_classes())) {
// Get rid of php extension easily without pathinfo
$classNoExt = substr($classExt, 0, -4);
$file = static::$path . $classExt;
if($requireInterface) {
// Get interface file
$interface = static::$interfacePath . $classExt;
// Check if interface file exists
if(!file_exists($interface)) {
// Throw exception
die("Unable to load interface file: " . $interface);
}
// Require interface
require_once $interface;
//Check if interface is set
if(!interface_exists("Interface" . $classNoExt)) {
// Throw exception
die("Unable to find interface: " . $interface);
}
}
// Require class
require_once $file;
// Check if class file exists
if(class_exists($classNoExt)) {
// Set class // class.container.php
static::$library[$classNoExt] = new $classNoExt();
} else {
// Throw error
die("Unable to load class: " . $classNoExt);
}
}
}
}
}
/*public function get($class)
{
return (in_array($class, get_declared_classes()) ? static::$library[$class] : die("Class <b>{$class}</b> doesn't exist."));
}*/
}
You can easily manage with a bit of coding, to require classes in different folders too. Hopefully this can be of some use to you.
You can specify a namespaces-friendly autoloading using this autoloader.
<?php
spl_autoload_register(function($className) {
$file = __DIR__ . '\\' . $className . '.php';
$file = str_replace('\\', DIRECTORY_SEPARATOR, $file);
if (file_exists($file)) {
include $file;
}
});
Make sure that you specify the class file's location corretly.
Source
spl_autoload_register(function ($class_name) {
$iterator = new DirectoryIterator(dirname(__FILE__));
$files = $iterator->getPath()."/classes/".$class_name.".class.php";
if (file_exists($files)) {
include($files);
} else {
die("Warning:The file {$files}.class.php could not be found!");
}
});
do this in a file and called it anything like (mr_load.php)
this were u put all your classes
spl_autoload_register(function($class){
$path = '\Applicaton/classes/';
$extension = '.php';
$fileName = $path.$class.$extension;
include $_SERVER['DOCUMENT_ROOT'].$fileName;
})
;
then create another file and include mr_load.php; $load_class = new BusStop(); $load_class->method()

PHP file_exists returning false

I'm trying to write my own little MVC framework for my projects, something I can just drop in and get up and running quickly, mostly for learning purposes. Every request gets routed through index.php which has this code:
<?php
// Run application
require 'application/app.php';
$app = new App();
$app->run();
This is my application class:
<?php
class App {
public function run() {
// Determine request path
$path = $_SERVER['REQUEST_URI'];
// Load routes
require_once 'routes.php';
// Match this request to a route
if(isset(Routes::$routes[$path])) {
} else {
// Use default route
$controller = Routes::$routes['/'][0];
$action = Routes::$routes['/'][1];
}
// Check if controller exists
if(file_exists('controllers/' . $controller . '.php')) {
// Include and instantiate controller
require_once 'controllers/' . $controller . '.php';
$controller = new $controller . 'Controller';
// Run method for this route
if(method_exists($controller, $action)) {
return $controller->$action();
} else {
die('Method ' . $action . ' missing in controller ' . $controller);
}
} else {
die('Controller ' . $controller . 'Controller missing');
}
}
}
and this is my routes file:
<?php
class Routes {
public static $routes = array(
'/' => array('Pages', 'home')
);
}
When I try loading the root directory (/) I get this:
Controller PagesController missing
For some reason the file_exists function can't see my controller. This is my directory structure:
/application
/controllers
Pages.php
/models
/views
app.php
routes.php
So by using if(file_exists('controllers/' . $controller . '.php')) from app.php, it should be able to find controllers/Pages.php, but it can't.
Anyone know how I can fix this?
You are using relative path's for your includes. As your application grows, it will become a nightmare.
I suggest you
write a autoloader class, that deals with including files. Use some mapping mechanism that translates namespaces / class names into path's.
use absolute paths. See the adjusted code below.
Example:
// Run application
define('ROOT', dirname(__FILE__) );
require ROOT . '/application/app.php';
$app = new App();
$app->run();
And later:
// Check if controller exists
if(file_exists(ROOT . '/application/controllers/' . $controller . '.php')) {
// Include and instantiate controller
require_once ROOT. '/application/controllers/' . $controller . '.php';
$controller = new $controller . 'Controller';

Categories