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

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

Related

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.

How can I call a function in a php class?

This is a sample code:
sample code
I want to call it in another page:
include 'root of class file';
$r = new ImagineResizer();
I got this error:
Fatal error: Class 'ImagineResizer' not found in C:\WampDeveloper\Websites\example.com\webroot\images.php on line 13
Also call the function:
$r->resize('c:/test1.jpg', 'c:/test2.jpg');
As seen in your sample code the class is located in another namespace :
<?php
namespace Acme\MyBundle\Service;
use Symfony\Component\HttpFoundation\File\File;
use Imagine\Image\ImagineInterface;
use Imagine\Image\BoxInterface;
use Imagine\Image\Point;
use Imagine\Image\Box;
class ImagineResizer {
//-- rest of code
}
To use a class in another namespace you need to point out where the file is :
First include the class (manual or with autoloading)
Then u can create an instance in 2 ways. First way with the use-keyword
use Acme\MyBundle\Service\ImageResizer;
$object = new ImageResizer();
Or point to the class absolute :
$object = new \Acme\MyBundle\Service\ImageResizer();
Hopefully, this will help you out some:
Make sure you include the actual file - not just the folder where it lies.
Make sure that the file you're calling the class from uses the same namespace as your class file. If it doesn't, you have to call the class using the full namespace.
Profit.
The namespaces really had my patience go for a spin when I started using them, but once you're used to it it's not too hard. I would recommend using an autoloader though. It's a bit of a hassle to set up, but once it's done it helps out a bunch.
Namespaces: http://php.net/manual/en/language.namespaces.php
Autoloader: http://php.net/manual/en/function.spl-autoload-register.php

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

Using namespaces in PHP

I am working on the AWS documentation which uses Guzzle framework. I have to deal with namespaces here and I am not able to get it working. I went through the docs and examples and understood that we can have packages for projects using namespaces.
I went ahead and tried a simple example, but unsuccessful. Here's the example: this is the index.php:
use My\Full\Classname as Another; //Also tried use My\Full\Classname
$obj = new Another; //with $obj = new Classname;
echo $obj->add();
I have Classname.php in the directory structure like this My->Full->Classname.php:
<?php
class Classname{
public static function add(){
return 2+2;
}
}
?>
I am trying to call the function in index.php but getting error:
Fatal error: Class 'Another' not found in C:\wamp\www\guzzleEx\index.php on line 19
which is the line where I instantiate the Classname object $obj = new Another;
What is the mistake i am making? Is there any INI that needs to be updated or any other config issue? How can I make the code working? If you use the normal include for Classname.php it works fine.
Namespaces need to be explicitly declared, they do not come from a certain directory structure.
So if you do not have a line that reads namespace My\Full; in front of your class Classname, then your class is not in any namespace, but in the root namespace.
Thus you cannot use it as \My\Full\Classname, but \Classname or even Classname directly.

Categories