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.
Related
I have this in my controller:
if (!defined('BASEPATH'))
exit('No direct script access allowed');
use xampp\htdocs\client\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet;
use xampp\htdocs\client\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx;
But here is the error I am seeing after running the code
Message: Class 'xampp\htdocs\client\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet' not found
Filename: C:\xampp\htdocs\client\application\controllers\admin\Home.php
You appear to have confused use and include/require.
A use statement is for namespace importing and aliasing. It says "when I use the class name Foo, what I actually mean is Something\Something\Foo. That full name may look like a Windows file path, but the \ here is actually PHP's namespace separator, and doesn't directly relate to the location on disk.
In this case, you would write:
// Alias these class name so I don't have to write them in full in this file
use PhpSpreadsheet\Spreadsheet;
use PhpSpreadsheet\Writer\Xlsx;
If you want to reference the code in a particular file, you need the include and require family of keywords. Those say "load this PHP file, and execute the code in it, including class and function definitions.
So the following would make sense:
// Load the file
require_once 'xampp\htdocs\client\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet.php';
require_once 'xampp\htdocs\client\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx.php';
However, most PHP libraries are built to be autoloaded, so you don't have to name each file manually. Generally, you don't even need to configure the autoloading itself, instead you'd use Composer to install them, and it would set up the autoloader for you.
You would then write, in the main entry point of your code:
require_once 'vendor/autoload.php';
And the classes would be loaded automatically when referenced. Note that you probably still want the use lines, though, and those do have to be in each file (because each file can use the same alias to reference different classes).
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.
Hey everyone I'm making a little webiste with the silex php microframework and I have a problem that I can't understand! So here is my question !
If I use this at the top of my main php file :
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
and then I later require these files :
require 'phpFiles/login.php';
require 'phpFiles/register.php';
Why can't i use the top classes in the code of my required class ?
I dont want to add these 2 use line to every single php file I require
Hope you guys understand my question !
It's not like you "can't use the top classes" in the included files. It's just that the included files don't inherit the parent file's importing/aliasing rules defined with use because those are per-file basis only. If you want to be able to refer to Symfony\Component\HttpFoundation\Request with just Request in the included files, you need to use use in those as well. Your only other option is using fully qualified names.
I think this is very simple for many of you, but in the moment I got stuck with this. I have the following part of code:
header.php
include "facebook/autoload.php";
use Facebook\FacebookRedirectLoginHelper;
test.php
include "header.php";
$helper = new FacebookRedirectLoginHelper($redirect_url);
Why do I always get this error:
Fatal error: Class 'FacebookRedirectLoginHelper' not found in test.php on line
I thought when I include a PHP file, classes can also be used. But in this case not, why? I think I do not understand how this autoload and use works, so I would be happy for some explanation.
PHP does not inherit the namespaces nor the use statements of included/required files. This is intentional as otherwise if you include 2 files using a class aliased the same way you will get errors and you might not need all those classes in firs place.
If a class requires a namespace it has to have use statement defined with the full namespace to the particular class they need. Except in the cases where there might be aliasing. For example if you have:
// file1.php
use \My\Cool\LogWriter as Writer;
and
// file2.php
use \My\Cool\FileWriter as Writer;
Now both classes are accessible as Writer.
// test.php
require 'file1.php';
require 'file2.php';
In which case if you don't declare which class from which space you want this will give nasty error that class Writer is defined, which is true, but it is also true that the two classes are 2 separate ones.
For more information on namespaces in PHP5 see (http://php.net/manual/en/language.namespaces.php).
As a side note:
Every file, if not namespace declaration is provided is considered in the global namespace.
If a use is without leading slash the namespace might be considered relative to the current. (unsure but I think it depends on the autoloader?) (Reference here: https://stackoverflow.com/a/4879615/1747193)
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