new class from variable - php

I've got a problem. I'm using Laravel 5.4 and on initialization
$class = (string)$module->controller.'Controller';
$class = new $class();
$class->startModule($module->title,$request);
I get a response from server FatalErrorException in ModulesController.php line 29:
Class 'FileManagerController' not found
image
but on manual call it works fine
$class = new FileManagerController();
$class->startModule($module->title,$request)
Plese tell me what's the problem?

First require class and then create instance:
$class = (string)$module->controller.'Controller';
require_once $class . '.php';
$class = new $class();
$class->startModule($module->title,$request);

I've got solution
$class = (string)$module->controller.'Controller';
app('App\Http\Controllers\\'.$class)->startModule($module->title,$request);

Related

Instantiating a PHP class using a variable is not working

This is working:
$x = new classname();
This is not working:
$class = "classname";
$x = new $class();
The error I get is "Class classname not found". PHP version is 5.4.22. Any ideas? As far as I have researched into this topic this is exactly what you need to do in order to instantiate a class using a variable.
My actual testcode (copy+paste), $build = 1:
//include the update file
$class="db_update_" . str_pad($build, 4, '0', STR_PAD_LEFT);
require_once(__ROOT__ . "/dbupdates/" . $class . ".php");
$x = new db_update_0001();
$xyz="db_update_0001";
$x = new $xyz();
The class definition:
namespace dbupdates;
require_once("db_update.php");
class db_update_0001 extends db_update
{
...
}
I just found out that my editor added
use dbupdates\db_update_0001;
to the file. So that explains why "new db_update_0001();" is working. What i want to achieve is that I dynamically include database updates which are stored in files like dbupdates/db_update_0001.php
Regards,
Alex
You have to use the full qualified class name. Which is namespace\classname. So in your case the code should be:
$x = new db_update_0001();
$xyz="dbupdates\db_update_0001";
$x = new $xyz();
The use statement is useless if you like to instantiate a class by using a variable as classname.
Try this
<?php
$className = yourClassName();
$x = new $className;
?>

Zend Framework 2 Error ListenerOptions not found

I need help setting up Zend Framework 2. I am getting this error:
Fatal error: Class 'Zend\Module\Listener\ListenerOptions' not found in index.php
Why is the class not found?
This is my index.php:
<?php
chdir(dirname(__DIR__));
require_once './vendor/ZendFramework/library' . '/Zend/Loader/AutoloaderFactory.php';
Zend\Loader\AutoloaderFactory::factory();
$appConfig = include './config/application.config.php';
$listenerOptions = new Zend\Module\Listener\ListenerOptions($appConfig['module_listener_options']);
$defaultListeners = new Zend\Module\Listener\DefaultListenerAggregate($listenerOptions);
$defaultListeners->getConfigListener()->addConfigGlobPath("config/autoload/{,*.}{global,local}.config.php");
$moduleManager = new Zend\Module\Manager($appConfig['modules']);
$moduleManager->events()->attachAggregate($defaultListeners);
$moduleManager->loadModules();
// Create application, bootstrap, and run
$bootstrap = new Zend\Mvc\Bootstrap($defaultListeners->getConfigListener()->getMergedConfig());
$application = new Zend\Mvc\Application;
$bootstrap->bootstrap($application);
$application->run()->send();
?>
The Zend\Module\Listener\ListenerOptions is now under Zend\ModuleManager\Listener\ListenerOptions check this class and other related to the Namespace Zend\Module\Listener

How to use defined data as function or class name?

I defined a data like below..
define('INDEX_CONTROLLER', 'test');
And I want to use that like below..
require_once 'controllers/' . INDEX_CONTROLLER . '.php';
$this->controller = new INDEX_CONTROLLER();
I'm getting error that below..
Fatal error: Class 'INDEX_CONTROLLER' not found in
/var/www/own/boot.php on line 13
You can set it equal to a variable and then call it:
$controller = INDEX_CONTROLLER;
$this->controller = new $controller();
Better use Reflections
DEFINE('INDEX_CONTROLLER', 'test');
$rc= new ReflectionClass(INDEX_CONTROLLER);
$this->controller = $rc->newInstance();
or in one line if you use php5.4+
$this->controller = (new ReflectionClass(INDEX_CONTROLLER))->newInstance();
You can read more about Reflections here: http://php.net/manual/en/book.reflection.php

generate class name?

how can you do something like this?
require_once 'class.Table_'.$table.'.php';
$class = new Table_$table();
$className = $var.'someString'.$var2;
$obj = new $className();
In your case
$className = 'Table_'.$table;
$obj = new $className();
Pretty much the way you did it.
The variable has to be the whole class name.
So:
<?php
$clsName = "Table_a";
require_once "class.{$clsName}.php";
$class = new $clsName();
?>
This is supported since PHP 5.2.

PHP custom class loader

i made a custom class loader function in php
something like..
load_class($className,$parameters,$instantiate);
its supposed to include the class and optionally instantiate the class specified
the problem is about the parameters. ive been trying to pass the parameters all day
i tried
load_class('className',"'param1','param2'",TRUE);
and
load_class('className',array('param1','param2'),TRUE);
luckily nothing works xD
is it possible to pass the params?
i even tried..
$clas = new MyClass(array('param1','param2'));
here it is..
function load_class($class, $param=null, $instantiate=FALSE){
$object = array();
$object['is_required'] = require_once(CLASSES.$class.'.php');
if($instantiate AND $object['is_required']){
$object[$class] = new $class($param);
}
return $object;
}
if you are in PHP 5.x I really really recommend you to use autoload. Prior to PHP 5.3 you should create sort of "namespace" (I usually do this with _ (underscore))
autoload allows you to include classes on the fly and if your classes are well designed the overhead is minimun.
usually my autoload function looks like:
<?php
function __autoload($className) {
$base = dirname(__FILE__);
$path = explode('_', $className);
$class = strtolower(implode('/',$path));
$file = $base . "/" . $class;
if (file_exists($file)) {
require $file;
}
else {
error_log('Class "' . $className . '" could not be autoloaded');
throw new Exception('Class "' . $className . '" could not be autoloaded from: '.$file);
}
}
this way calling
$car = new App_Model_Car(array('color' => 'red', 'brand' => 'ford'));
the function will include the class
app/model/car.php
Seems to me that you should be using __autoload() to just load classes as they are referenced and circumvent having to call this method manually. This is exactly what __autoload() is for.

Categories