Using a library in php: class not found - php

I'm trying to use this library: https://github.com/duzun/hQuery.php
My project is ordonated like this:
BetCompare
Application
Teams
file_using_the_library.php
hQueryLib
hquery.php
So here is how I'm using it in my php file:
namespace BetCompare\Application\Teams;
use BetCompare\Application\Teams\hQueryLib\hquery;
hQuery::$cache_path = "/path/to/cache";
This returns an error Class not found. I've tried this after a few researches on the matter:
namespace BetCompare\Application\Teams;
use BetCompare\Application\Teams\hQueryLib\hquery;
include_once 'hQueryLib/hquery.php';
hQuery::$cache_path = "/path/to/cache";
Then the error is the following: Cannot declare class hQuery_Context, because the name is already in use. I don't understand, the second error makes it look like the use was enough and loaded the class. But I can't use it... What am I doing wrong?
I've also tried only using include_once but it doesn't work.

Either like this (with namespace duzun\hQuery):
<?php
namespace BetCompare\Application\Teams;
use duzun\hQuery;
include_once 'hQueryLib/hquery.php';
hQuery::$cache_path = "/path/to/cache";
or like this, without the namespace:
<?php
namespace BetCompare\Application\Teams;
include_once 'hQueryLib/hquery.php';
\hQuery::$cache_path = "/path/to/cache";
Your code can not work, because if you write
use BetCompare\Application\Teams\hQueryLib\hQuery;
you are practically assuming, that the hQuery class has the namespace definition
namespace BetCompare\Application\Teams\hQueryLib;
which it does not have (beeing a third-party class).
The part with the duzun\hQuery is defined in the last lines of the hQuery.php file and described in the docus.

Related

How to include / require `use` operator statements?

I dynamically generate my use statements and try to include them.
use.php (Path: /App/Test/Use/use.php)
<?php
use App\Http\Utility\GeneralUtility;
use App\Models\Project;
use App\Http\Selenium;
?>
PlayController.php (Path: /App/Http/Controllers/PlayController.php)
<?php
namespace App\Http\Controllers;
require app_path() . '\Projects\Test\Use\use.php';
...
However, If I try to use a included/required class in my controller then I get the info that some classes are missing. E.g.
FatalThrowableError in PlayController.php line 30:
Class 'App\Http\Controllers\Selenium' not found
Of course it works If I write them manually into the Controller without using require:
<?php
namespace App\Http\Controllers;
use App\Http\Utility\GeneralUtility;
use App\Models\Project;
use App\Http\Selenium;
...
So why does it not work if I include / require them?
It is not possible what you are trying to do.
Here is the documentation from the PHP manual on it:
Note: Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.
The source comes from here and the full documentation link is here.
Use is done at compile time and include at runtime, so that makes it impossible.
But what you could do is, use something like PHP-Parser to replace the includes before the script is being executed.

Yii2 -windows 10 xampp path error

here i have a Crypt.php file . its a class have two function, and stored it in "\backend\components" folder. i call this file(class) in my controller using this code
$security = new \backend\components\Crypt();
at run time i am getting this error:
"Unknown Class – yii\base\UnknownClassException
Unable to find 'backend\components\Crypt' in file: E:\xampp\htdocs\pope-Admin/backend/components/Crypt.php. Namespace missing?"
in this path half of them have slash(/) and half of the part have back slash() how to solve it?
In your Crypt class file, include namespace declaration like so:
<?php
namespace backend\components;
class Crypt {
...
}
?>
Use the include(_once) or require(_once) keywords to include the Crypt.php file, then just use new Crypt(). You cant define an instance of a class like that, you have to include the filw containg the class code and only then you can use the new keyword.

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

PHP namespaces trouble

I just wanna use a namespace in another file, to use the class in it, but im too retarded to do this...
first file:
namespace fun;
use fun\kjr\trouble;
$trouble = new trouble('http://someURL');
second file:
namespace fun\kjr;
class trouble { ... }
This is the error i get:
Error: Fatal error: Class 'fun\kjr\trouble' not found in D:\wamp\www\fun\index.php on line 8
Where did I declare a wrong namespace? Oo
Greetings
I had to include my class aswell.
use namespace
only makes the class available in the actual context.
It does NOT include it, so you can cause it.
use
include_once('your.file')
to do that!
Thanks #Michael Berkowski for helping.
The problem (from what I can see) is you haven't specified an autoloader (e.g. with spl_autoload_register()) or haven't included the file in your code. The use statement will only find a class for you if you have an autoloader in place, otherwise you must include your code ahead of time with include/include_once or require/require once.
I would start with making an autoloader and registering it and then it may be wise to map your namespaces to directory structure as specified by PSR-0.

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
}
}

Categories