I have been trying to extend the class of h5p renderer but to no avail.
I guess there could be tons of 'renderer' classes in the system, that is why I get the error.
What I do:
require the main class file:
require_once($CFG->dirroot . '/h5p/classes/output/renderer.php');
The class looks like this:
namespace core_h5p\output;
use plugin_renderer_base;
class renderer extends plugin_renderer_base {
Then in my theme I try to extend this class:
class theme_mytheme_renderer extends renderer {
but this generates an error Exception - Class 'renderer' not found
How can I make it work?
Related
I have these folder structure:
The Application.php looks so:
<?php
namespace Mensa;
class Application extends \Application\Application{
public function getLayoutViewScript()
{
return __DIR__.'/../../view/index.php';
}
}
My problem is that I want to extend a class in my Application Class in the Mensa Namespace. The class I want to extend is located in Application/Applivation.php (has the same name).
But when I run I get the errot "Fatal error: Class 'Application\Application' not found..."
So how can I inherit the class in Application folder?
Thx!
I would like to load a Class that I have located in src/Bandit/EpsilonGreedy. EpsilonGreedy itself extends an abstract class located in the same Folder src/Bandit/ABandit.
When I try to load the class with new App\Bandit\EpsilonGreedy(); I nor get an error message nor an instance of the class.
The namespace on top of both PHP-Files i have choosen is namespace App\Bandit;.
Do I have to bypass the autoloader of CakePHP to instantiate such classes in own Folders beside Controller, Model and View?
Thanks!
I have a class named Service_B which extends a custom service class.
This custom service class requires one single object named Reader in its __construct() in order to instantiate properly.
The parent service is defined as follow
namespace Vendor\Services;
abstract class Service{
function __construct(Vendor\Services\Reader $reader){
}
}
Service_B is defined as follow:
namespace Vendor\Services;
class Service_B extends Service{
function __construct(){
parent::__construct(new \Vendor\Services\Reader());
}
}
Reader do have the following line at the top of the file:
use Vendor\Services;
Class files are organized like this:
Vendor/Services/Service_B.php
Vendor/Services/Reader.php
Question:
When I instantiate Service_B, I get the following error message:
Fatal error: Class 'Vendor\Services\Reader' not found
I don't understand why I get this error since I think I am using the proper namespaces declarations. Thank you
On top of your Reader class place:
//This will declare the Reader class in this namespace
namespace Vendor\Services;
and remove:
//THIS IS A WRONG DIRECTIVE: you're telling PHP to use the Vendor\Services class but it doesn't even exist
use Vendor\Services;
Then modify the Service_B class as follow:
namespace Vendor\Services;
//i think this should extend Service, as it's calling the parent constructor
class Service_B extends Service
{
function __construct(){
parent::__construct( new Reader() );
}
}
This way all yours 3 classes will be in the same namespace, and the Reader class should be found without explicit namespace prefix
This question already has answers here:
Codeigniter extending extended MY_Controller
(5 answers)
Closed 8 years ago.
class Settings extends USER_Controller {...}
class USER_Controller extends MY_Controller {...}//limit access to user and define some params and functions that is user depended
class MY_Controller extends CI_Controller {...}//provide extra functionality accross all controllers
well if i try to create above 3 class's this will not work.
yet if i just use settings extends MY_Controller it will work fine.
so is there a way i can put a middle class between my controller and MY_Controller -the base controller that extends CI_Controller ?
As #Minhaz Ahmed said, the problem is loading the class. Codeigniter only loads the class with the subclass prefix as in,
https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/CodeIgniter.php#L276
if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
{
require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
}
So after some diggings I have came with a solution using both codeigniter hooks and spl_​autoload_​register() for my project without hacking the CORE. Please follow the steps to achieve what's in THE QUESTION.
Enable hooks in the config file if it isn't already.
create a hook in application/config/hooks.php as below.
$hook['pre_system'] = array(
'class' => '',
'function' => 'autoload',
'filename' => 'autoload.php',
'filepath' => 'hooks',
'params' => ''
);
Create the autoload.php in application/hooks folder.
inside the autoload.php file,
function autoload()
{
spl_autoload_register(function($class)
{
if(strpos($class,'CI_') !== 0 && file_exists(APPPATH.'core/'.$class.EXT))
{
require_once APPPATH . 'core/' . $class . EXT;
}
});
}
That's it.
Note: Here, I've used pre_system hook, not pre_controller, since codeigniter loads the base controller classes between pre_system and pre_controller hooks.
Hope this helps. If there are any issues with this please do comments.
Place the user_controller class file at the end of MY_Controller.php
MY_Controller.php in /application/core/
class MY_Controller extends CI_Controller {...}
class USER_Controller extends MY_Controller {...}
Now from your controller in your controllers folder, you can extend the controller from USER_Controller:
class Settings extends USER_Controller {...}
What did the best job for CI 2.1.4:
application/core
create for example:
- base_controller (base instructions, extending from ci_controller)
- admin_controller (extending from basecontroller and adds authentication instructions)
Then use them in: application/controllers:
for example:
class users extends admin_controller...
You could use Phil's core solution, its simple and easy.
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
You can extend the class the way you want but the problem is loading the class, you have to include your USER_Controller your Settings Class. Problem is not extend problem is including the file, so you have to find a way to include the second class.
See http://www.php.net/manual/en/function.spl-autoload.php
I'm trying to integrate PHP namespaces into an existing Zend Framework project (v1.12). When I add namespacing at the top of a working controller, it doesn't work anymore and the application throws an Invalid controller class error. Here's my controller definition :
namespace MyProject\Controller;
use MyProject\Controller\MyRestController;
class MyFooController extends MyRestController
{
}
and the init method within the Bootstrap.php:
protected function _initAutoload()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyProject');
return $autoloader;
}
Just a guess (have not used ZF for quite some time): Zend will not accept any class as a controller, just those extended from the framework's base controller class. As you don't extend from the frameworks base controller class you see the error.
If that is the reason, take care you initially extended from the base framework controller class or you implemented the needed interface.
namespace MyProject\Controller;
class MyRestController extendes Zend_Framework_Base_Controller_Class_Name_Here
{
...
p.s. the use MyProject\Controller\MyRestController; looks superfluous as that class is in that namespace already. Let's review your code:
namespace MyProject\Controller;
This sets the namespace of the file. That means, non-FQCN will resolve into it. For example:
new MyRestController();
Resolves to the following FQCN:
new MyProject\Controller\MyRestController
Which - oha! - is exactly what you wrote in use:
use MyProject\Controller\MyRestController;
Which means, that this use clause is superfluous, the extend in:
class MyFooController extends MyRestController
Would go to it anyway at first. Because it's the same namespace.
I am facing similar problem now. For me this looks like that Zend cannot properly resolve namespaced controller name. So when I put for example IndexController into namespace \Basic\Controller, it will be not loaded because Zend want to load \IndexController class, which does not exist.
I am thinking about extending standard zend router class, which has method getControllerName.
Then I can set this in bootstrap by:
$router = new \My\Namespaced\Router();
$front = Zend_Controller_Front::getInstance();
$front->setRouter($router);
I didn't tried that code yet but this should work.