Calling Elastica class using "_" instead of "\" - php

I'm using Elastica search engine for my Symfony project.
Now, I'm getting the below error :
The autoloader expected class "Elastica_Query_Bool" to be defined in
file
"/blablabla/vendor/ruflin/elastica/lib/Elastica/Query/Bool.php".
The file was found but the class was not in it, the class name or
namespace probably has a typo.
If I change new \Elastica_Query_Bool() for new \Elastica\Query\Bool() in my php file, it works fine.
But I can't understand why I'm getting an error now. Any idea ?

Because when you new Elastica_Query_Bool it's looking for a class actually called Elastica_Query_Bool. And of course the actual class is called Bool.
try:
use Elastica\Query\Bool; // At the top of your file following the namespace line.
...
$bool = new Bool();
Might want to review namespaces in the php manual.

Related

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

Zend framework 2 Zend\config not found

I have an issue on Zf2 when i try to create an xml document, using :
$config = new Zend\Config\Config(array(), true);
i followed the official documentation here :
Zend framework 2 Zend\Config\writer
I'm sure zf2 is loader and i don't understand what happening there but.
My output is
Fatal error: Class 'XmlGenerator\Controller\Zend\Config\Config' not found in C:\wamp\www\myLink\module\XmlGenerator\src\XmlGenerator\Controller\XmlGeneratorController.php
if someone can explain please, i'm lost ! :p
Thanks in advance !!
You are having a namespacing problem.
You need to either include the Zend namespace with use Zend; after where you declare the name space or change your code to $config = new \Zend\Config\Config(array(), true);
PHP is looking for class in your current namespace so it is adding the current namespace to fill name of the class (in this case "XmlGenerator\Controller") which the autoloader uses to determine which directory to get the class from. Since the Zend code isn't in the same directory as your controller, the autoloader chokes and gives you the error.
You need to tell your code that you are also using the Zend namespace (via use) or that the class you are using is in the global namespace (prepending the \ to the class name)

functions accessible by multiple php classes (Symfony controllers)

I have a couple functions that are used in multiple controllers in my Symfony project. Instead of copy and pasting them constantly, I'd like to have a separate php file (say functions.php) to store the functions.
I've tried the following:
1) include and require the php file with the functions, but it won't let me use $this->getDoctrine().
I've looked at the following posts for help, but it didn't solve the issue:
Symfony2: get Doctrine in a generic PHP class
Symfony2 Use Doctrine in Service Container
2) I've tried making it a class and having the functions as methods then doing:
$f = new Functions();
$variable = $f->myMethod();
But it returns:
The file was found but the class was not in it, the class name or namespace probably has a typo.
Thanks for any help. I really appreciate it.
UPDATE:
The generic class is Symfony/src/WikiRoster/MainBundle/Controller/DBFunctions.php
I just need to be able to use $this->getDoctrine() or the like somehow in there now. I've tried using the services.yml as suggested in the link above, but no cigar. Thanks!
The file was found but the class was not in it, the class name or
namespace probably has a typo.
This issue happens quite often actually. As the error mentions, there are 2 cases explaining why this error is returned:
If the namespace of your file is not the same as the path of your file
If the name of your file is not the same as the name you are using in your class
Example
Let's say you would like to use a GeneralClass in a different class like that:
use Acme\YourBundle\General\GenericClass
For this to work, GeneralClass needs to be in Acme/YourBundle/General/, and you need to:
use the correct namespace: namespace Acme\YourBundle\General
name your class with the same name: class GenericClass
You would have:
namespace Acme\YourBundle\General;
class GenericClass
{
public function __construct()
{
// some cool stuff
}
}

Fatal Error RecursiveIteratorIterator not found

As the title says, when I instantiate a class I get this message :
Fatal error: Class 'Envato\RecursiveIteratorIterator' not found in C:\Users\rgr\Apache\htdocs\Roland Groza [ 3.0 ]\class\envato\envato.php on line 359
You can view the class here : Class ;
I'm instantiating from another file :
require("envato.php");
$test = new Envato\EnvatoAPIWrapper();
echo "User Vitals : ".$test->get_user_vitals("chaoscod3r")."<br>";
The class is wrapped with a namespace, so that might have something to do with it, but I wasn't sure since it's been a few years since I haven't coded PHP. Hopefully someone has an idea what is it that I'm doing wrong :)
To access non-namespaced classes like the internal classes of PHP and SPL inside of a namespace you have to use the fully qualified class name like this:
new \RecursiveIteratorIterator();
or import it explicitly at the beginning:
use \RecursiveIteratorIterator;
and then use it normally like you do.
Add a use statement at the top of your namespace...
use \RecursiveIteratorIterator;
If you don't then PHP expects RecursiveIteratorIterator to exist within your current namespace, rather than in the global namespace (indicated by the leading \)

Categories