How to properly use Universal Class Loader? - php

I'm trying to use Universal Class Loader functionality from Phalcon into my project, however I can't get it working.
Here is how i've implemented into my app (using registerClasses).
index.php:
//...
$loader->registerClasses(
array(
"Commons" => "library/classes/CommonsClass.php"
)
);
$loader->register();
sampleController.php:
public function doAction()
{
$cc = new Commons();
}
And when I execute the controller, it throws me this exception:
Fatal error: Class 'Commons' not found in C:\the\path\to\phalcon_app\app\controllers\SomeController.php on line 63
The Phalcon Documentation just say that you need to register a class and call it in your funcion. There is something I've missed?
Ps.: The library folder is not registered anywhere (don't know if its needed), and it is in the same path as controllers, views, etc (/app/).

You should probably check your directory structure.
given:
mah_app/app/config/loader.php
mah_app/library/classes/Commons.php
I would expect this to work:
// loader.php
$loader->registerClasses(
array(
"Commons" => __DIR__ . "/../../library/classes/Commons.php"
)
)->register();
// works with $loader->registerDirs() for sure
Also, I would also suggest using namespaces and/or matching file name with class name.

Had the same problem. I commented out the namespace declaration inside the class and it worked. Version 2.0.3.

Related

Namespace conflict when using 3rd party api in class

There is something weird going on. I'm a novice with respect to php programming.
I'm trying to use FirePHP but my question is actually not related to that tool directly.
The function fb you'll see below is part of FirePHP. My entry point is Main.php. The first fb call is executed without any problems but the second call (see ExperController.php) ,which gets triggered when $ec->exper(); in Main.php is called, causes a fatal error:
Fatal error: Uncaught Error: Class 'App\Controllers\FirePHP' not found in path-to-wp-directory\wp-content\mu-plugins\typerocket\app\Controllers\ExperController.php on line 12
Why is the runtime engine looking for the class FirePHP under that namespace (App\Controllers)? And why wasn't this an issue during the first fb call? There isn't any namespace defined in the FirePHP files at all. And my last but crucial question, how can I fix this without having to touch 3rd party files? This answer is not a solution for my case since I'm not referencing / calling the mentioned class in my code!
Note: FirePHP is included via autoloader (provided by composer).
Main.php:
<?php
fb('Hello World!', FirePHP::INFO);
$req = new \TypeRocket\Http\Request();
$res = new \TypeRocket\Http\Response();
$ec = new \App\Controllers\ExperController($req, $res);
$ec->exper();
ExperController.php:
<?php
namespace App\Controllers;
use TypeRocket\Controllers\Controller;
class ExperController extends Controller
{
public function exper() {
fb('Hello World!', FirePHP::INFO);
}
}
do you use any Dependency manager like composer??
when you call it from main somehow php can see where is FirePHP class is and everything works fine but when you try to access it inside a class in another folder you must define where the class is inside your controller , like how you defined where is controller class that you extend :
use TypeRocket\Controllers\Controller;
so add FirePHP class to your controller and everything will be ok then.
use Path\To\Class\FirePHP;
also if you did all and still not working you may try dump autoload;
composer dump-autoload

Symfony and plain php autoload class

I have app with old code - plain php and new code - Symfony. And when in old code call class symfony autoloader, maybe, intercepts class, how told Symfony to call exctly needed class
I have app/old/core/classes/Locale.php from old code, without name space and
app/src/AppBundle/Entity/Locale.php from new code, this is entity with name space
and in old code I call static function Locale from old code, but Symfony call entity, of course entity LOcale dont have this static function and I have error
class Page
{
function replacePageTags($replace_arr)
{
// set brases for keys
$braced_arr = Array();
foreach($replace_arr as $key => $value) {
$braced_arr["{".$key."}"] = $value;
}
//this I have error
$this->content = Locale::replaceTags($this->content, $braced_arr);
}
}
error message:
Attempted to call an undefined method named "replaceTags" of class "Locale".
whe I add include Locale to class Page
include('Locale.php');
class Page
{
have error
Error: Cannot declare class Locale, because the name is already in use
But when I add to entity static function replaceTags($text, $replace_arr) still have error, maybe Symfony call another Locale, but I need call Locale from old functionality, how can I do this ?
maybe this because in composer in autoload I have
"autoload": {
"psr-4": {
"": "src/"
}
but old code not in src or I dont know
In another developer have not this problem, I have php version 7.0.14, they have 7.0.8, maybe I need low some ph destriction, I don't know (
How to call exectly needed class in old code or fix this problem?
Your Page class uses \Locale from intl extension.
The only option you have is to disable the extension as the legacy (namespaceless) code is not compatible with it due to class name collision.
If your old Library does not have namespace, you can use MapClassLoader to do a mapping.
require_once '/path/to/src/Symfony/Component/ClassLoader/MapClassLoader.php';
$mapping = array(
'Foo' => '/path/to/Foo',
'Bar' => '/path/to/Bar',
);
$loader = new MapClassLoader($mapping);
$loader->register();
You can do it automatically on your old code by using the class map generator.
This way you could have a custom namespace like Legacy\Class for all your old classes

Organise custom classes

I'm trying to learn the basics of Slim 3 and I have difficulties trying to figure out the proper way to organise my custom code, esp. custom classes. For instance, I want to create a custom error handler:
<?php
namespace App\Handlers;
// [...]
final class Error extends \Slim\Handlers\Error
{
// [...]
}
... but the documentation I've checked hasn't revealed under which path to save the class definition or how to configure the framework so it can be found in my index.php entry point:
<?php
require __DIR__ . '/../vendor/autoload.php';
// [...]
$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);
$container = $app->getContainer();
$container['errorHandler'] = function ($c) {
return new App\Handlers\Error($c['Logger']);
};
Fatal error: Class 'App\Handlers\Error' not found
I'd appreciate any hint.
You're problem is not related to the framework at all.
Slim doesn't tell you where to keep your custom code because it's a matter of your free choice.
Your error:
Fatal error: Class 'App\Handlers\Error' not found
is not generated by Slim, but PHP itself. You need to add an autoloader for your code to let PHP know where to find appropriate classes.
I can see that you utilize Composer, therefore it's the best option to configure composer.json to create autoloader for your code.

Trouble with Namepaces and Class Loading

Im building a custom CMS and have setup an autoloader, and have adapted use of namespaces. For the most part things are loading properly, but in certain cases PHP reports that it cannot find the class, the class file has been included.
Once a file is included (using require), it should be instanced as well.
The parent controller is instanced, then the child controller attempts to instance a few of its own dependencies.
$this->auth = new \Modules\Auth\RT\Auth();
This will look for a file at /modules/auth/rt/auth.php, and it does and the class is instanced properly.
The namespace of Auth is:
namespace Modules\Auth\RT;
The auth class tried to load its own dependencies, a model in particular.
$this->auth_model = new Models\ModelAuth();
Here the file to be included is at /modules/auth/rt/models/modelauth.php
It is included successfully, but this is where PHP says I cannot find this class.
Fatal error: Class 'Modules\Auth\RT\ModelAuth' not found in /Users/richardtestani/Documents/ShopOpen-Master/shopopen/modules/auth/rt/auth.php on line 12
What would cause the class from not being instanced even though the file is included?
Thanks
Rich
try this:
$this->auth_model = new Modules\Auth\RT\Models\ModelAuth();
try this:
$this->auth_model = new \Modules\Auth\RT\Models\ModelAuth();
OR
$this->auth_model = new Models\ModelAuth();
when you are in this namespace \Modules\Auth\RT
There was a Missing \ , so the code trys to include the namespace twice;
FOR REAL ;)

Zend Framework and nested sets

we`re currently developing our own Feedmanager for ShopProducts based on Zend Framework.
In this feedmanager you can manage several feeds, to be created and sent to different locations.
Each feed has n-fields which are in the current version only in one level.
Now we want to change it to a multilevel version, we we have parentfields and childfields.
We dont want to reinvent the wheel, so we would like to use a developed class or helper.
I found the class of F. Pietka.
https://github.com/fpietka/Zend-Nested-Set
I tried to use this helper in our system, but had no success.
I wrote the following line, to use the class:
$oNested = new NestedSet_Model();
I got the following error message:
Fatal error: Call to undefined method NestedSet_Model::getDbTable() in
D:\xampp\htdocs\feedmanager_alpha\application\classes\NestedSet.class.php
on line 75
In the readme of Peitka`s NestedSet helper it says that the ZendLibrary needs to be in the include path.
I think I did this with the following lines:
define("ROOTPATH", realpath("../"));
define("LIBPATH", ROOTPATH . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR);
// Ensure library/ is on include_path
set_include_path(
implode(PATH_SEPARATOR,
array(
realpath(LIBPATH)
)
)
);
Did I set the include path correctly?
What could be the problem in my case?
Is a nested-set the best way to create the parent-child-model?
I would strongly advise you not to use this class, because it's clearly a WIP. However, if you want to circumvent this error, you can just delete the __construct() method and call the setDb() and setTableName() by hand.
Example:
$model = new NestedSet_Model();
$model->setDb(Zend_Db_Table::getDefaultAdapter());
$model->setTableName('table_name');
class NestedSet extends Zend_Db_Table
{
protected $_name = 't_nested_set_mkb_10';
public function set(){
$model = new NestedSet_Model();
$model->setDb(Zend_Db_Table::getDefaultAdapter());
$model->setTableName('t_nested_set_mkb_10');
}
}
It was long overdue, but I updated https://github.com/fpietka/Zend-Nested-Set (even with unit tests).
Feel free to create issues!

Categories