Symfony and plain php autoload class - php

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

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

What is the correct way to reference a sub-class of a PHP package when using Composer?

I have an application running in CakePHP 3 and have installed my packages via Composer.
Most recently I added phpspec/php-diff (https://packagist.org/packages/phpspec/php-diff) and ran composer update. It has put the files, as expected, in vendor/phpspec/php-diff/
I can instantiate the class in one of my CakePHP Controllers like this:
// src/Controller/UrlsController.php
// ...
use Diff;
public function test()
{
$diff = new Diff(foo, bar);
// This works
}
However, the documentation for this package gives this as an example on https://github.com/chrisboulton/php-diff/blob/master/example/example.php#L43 which comes after instantiating Diff (as done in test() above).
require_once dirname(__FILE__).'/../lib/Diff/Renderer/Html/Inline.php';
$renderer = new Diff_Renderer_Html_Inline;
echo $diff->render($renderer);
Obviously the require_once statement doesn't work because that's not where Inline.php lives in Composers hierarchy.
So how do you instantiate new Diff_Renderer_Html_Inline? If I just use it like this it errors saying
Class 'App\Controller\Diff_Renderer_Html_Inline' not found
Another workaround I thought may be to change the path in require_once so it points to it's location in vendor/. But surely there is a better solution?
If you're trying to refer to a class from within another namespace you need to refer to it using its fully qualified name e.g.
new \Diff_Renderer_Html_Inline;
Or add a use Diff_Renderer_Html_Inline after the namespace declaration to be able to do : new Diff_Renderer_Html_Inline

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 ;)

How to properly use Universal Class Loader?

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.

Include a php class -the old way- on Symfony 2

I need a fast way to test a code, please I know is not the best practice, but is there a way to load a class on a symfony controller without having to add namespaces, proxy class and autoloader stuff?
I tried:
public function singlepagestatsAction($id, $pid, $group) {
include '/var/www/web/welcomestuff/scripts/ga/gapi.class.php';
$ga = new gapi('email','password');
Inside my controller, but when i execute it; Fatal error: Class 'Done\PunctisBundle\Controller\gapi' not found in /var/www/src/Done/PunctisBundle/Controller/BrandController.php on line 630
You should add \ before class name
$ga = new \gapi('email','password');

Categories