I have a Symfony bundle where I will have to use a custom class. This class does not have to be accessible from all the website, but just in a controller of this bundle.
I have seen a few solutions relative to the vendors, but this is quite heavy and not necessary in my case.
Does someone have a simpler solution?
This is what namespaces are for.
From php.net:
What are namespaces? In the broadest definition namespaces are a way of encapsulating items.
Simply put, include your namespace at the top of your custom class.
src/Acme/DemoBundle/Model/MyClass.php
<?php
namespace Acme\DemoBundle\Model;
class MyClass { // ...
and use it in your Controller:
src/Acme/SomeOtherBundle/Controller/DefaultController.php
<?php
namespace Acme\SomeOtherBundle\Controller;
// ...
use Acme\DemoBundle\Model\MyClass; # can be used in any class in any bundle
// ...
class DefaultController extends Controller { // ...
Class must be created in Acme\Bundle\Model namespace and folder, not \Class! Who says this?!
// ../src/Acme/Bundle/Model/Utility.php
<?php namespace Acme\Bundle\Model;
class Utility:
..
Using in controllers:
<?php ..
use Acme\Bundle\Model\Utility;
$x = new Utility(); // FINE!:)
Name of file (Utility.php) must be equal to the class name.
Related
I'm trying to trick PHP into taking a class from another namespace when trying to create a specific class.
I have two class called "page", the first is in the Core namespace:
namespace Core;
class Page {...}
The second inherits from Core\Page, but adds a few things. It is in the Addons namespace.
namespace Addons;
class Page extends \Core\Page{...}
The reason I want to do this is because I want to build my system with an easy addon engine. Whenever I want, I can add a line in an XML file that tells the autoloading function to take the class in the addon namespace instead of the core namespace.
However, when I try to do this :
spl_autoload_register('loadClass');
public function loadClass(string $className)
{
if (Addon_exist_and_is_registered($className))
{
require "/Addons/$className.php";
}
else
{
require "/Core/$className.php";
}
}
$page = new \Core\Page(); <-- error here
I get an error saying that the class \Core\Page cannot be found in the file Addons\Page.php. This is normal behaviour since the class is not in the same namespace and as such, the fully qualified name cannot find the right class.
Is it possible to trick PHP into thinking that a child class in another namespace is actually the right class? I tried this for the addons class;
namespace Core;
class Page extends \Core\Page{...}
But it breaks the inheritance as you cannot inherit yourself.
Ignore that the classes have the "same name". Because they don't. One class is called Core\Page, the other is called Addons\Page. Those are their names, their fully qualified names to be exact. It's as much a difference as Foo and Bar. If you tell PHP to instantiate Core\Page, then it's going to do that; you can't "trick" it into instantiating Addons\Page, since that's an entirely different class name.
Don't try to "trick" anyone, make your system actually extensible and explicitly allow overriding of class names:
$class = 'Core\Page';
if (...) {
$class = 'Addons\Page';
}
$page = new $class;
I have a custom class I added to Classes, invoice.php within the Classes folder (\App\Classes).
When using a controller function, I can't execute a static function, says Class not found. I have the namespace and use set up like any other classes, similar to Helpers.php.
Class:
<?php namespace App\Classes;
class invoice {
//
}
?>
Contoller:
<?php namespace App\Http\Controllers;
use App\Classes\invoice;
class CustomerController extends Controller {
//
}
?>
Error:
FatalErrorException in CustomerController.php line 284:
Class 'App\Http\Controllers\invoice' not found
I've been stuck here for two hours and I don't understand what I'm doing wrong. Composer is using psr-4 and the Helpers.php file works fine, but my custom class file doesn't.
Thanks
To follow PSR-4 you need to have your class names initial-caps. The "i" should be capitalized here:
<?php namespace App\Classes;
class Invoice {
//
}
Also fix your use as well. Then when Invoice is used in your controller, it will pick up the correct namespace to use for resolving that class. What's happening currently is it is not finding a matching reference in your use section, so it assumes it is in the same namespace of the controller class that calls it.
I have a base abstract class, say Base.php located at /lib/Helper/Base.php. Now, I have another class, say Awesome.php located at /lib/Helper/Awesome/Awesome.php, and this class needs to extend Base.
I have defined my namespaces as follows:
Base.php
<?php namespace Helper;
abstract class Base
{
}
Awesome.php
<?php namespace Helper\Awesome;
class Awesome extends Base
{
}
Right now, this says that class Base was not found. I tried to use require_once and/or include to the path of Base.php and they also didn't work. What am I doing wrong?
You are including the file, and that is the right thing to do. But you are also using
namespaces.
the namespace is not a folder, that means the namespace 'Helper' is not a folder but a logical hierarchy. In order to use the class Base in he logical namespace Helper the Awesome class needs to extend the Base class with the full logical path i.e. \Helper\Base.
namespace Helper\Awesome;
class Awesome extends \Helper\Base {
}
or use the use/as keyword instead (like c# using statement) .
namespace Helper\Awesome;
use \Helper\Base as Base;
class Awesome extends Base
{
}
more information can be fount at the php.net site.
here is a direct reference: http://php.net/manual/en/language.namespaces.importing.php
I'm using php for my new project which is quite large. I feel the need to employ namespaces.
I have read many docs on it, but am confused practically about. Here is the scenario:
I have defined the following namespace and class:
namespace urlNS;
class URL
{
// content of the class
}
And then in another file, I want to define a class in the same namespace (urlNS) and also extend the URL class (which is a subset of urlNS namespace), and to do this, I have done:
namespace urlNS:
class Inc extends urlNS\url
{
// content
}
Now, I wanted to make use of a defined namespace twice (or even more), did I do it in a right way?
Base class definition:
<?php
namespace urlNS;
class URL {}
Subclass:
<?php
namespace urlNS;
class Inc extends URL {}
Using:
<?php
$url = new urlNS\URL();
$suburl = new urlNS\Inc();
Or with use keyword:
<?php
use urlNS\URL;
$url = URL();
I have been searching websites to try and get a handle on using PHP namespaces, but they all seem quite vague but what they're trying to do is easy to understand!
My question is: I have a file called people.php and in it is defined class called people. If I create another file called managers.php in the same folder can I define a class again called people which extends the original people class but in the namespace of managers, if so do I have to 'include' the original people.php and if so do I put the include after the writing: namespace managers?
Namespaces are a way to group your related classes in packages. What you describe could best be put under a single namespace like
<?php // people.php
namespace com\example\johnslibrary\people;
abstract class People {
}
and then
<?php // manager.php
namespace com\example\johnslibrary\people;
require_once 'path/to/People.php'; // can better use autoloading though
class Manager extends People {
}
because a Manager is a subclass of People, so there is not much of a reason to put them into their own namespace. They are specialized People.
If you want to Managers to be in their own namespace, you can do so, but have to use the fully qualified name when using the extends keyword, e.g.
<?php // manager.php
namespace com\example\johnslibrary\managers;
require_once 'path/to/People.php';
class Manager extends \com\example\johnslibrary\people\People {
}
or import the People class first
<?php // manager.php
namespace com\example\johnslibrary\managers;
use com\example\johnslibrary\People as People;
require_once 'path/to/People.php';
class Manager extends People {
}
See the PHP Manual on Namespaces for extensive documentation.
// people.php
<?php
namespace People;
class People {}
// managers.php
<?php
namespace Managers;
require_once __DIR__.'/people.php';
class People extends \People\People {}
I have old PHP Class and i need to use it in new PHP file as for example:index.php has to use iClass.php. But before using the OLD iClass.php i have to modify it as below, so that i can use it in index.php.
iClass.php:
namespace ic;
class iClass {
public static function callMeFromClass() {
echo 'OK - you have called me!';
exit;
}
}
index.php
namespace inex;
require_once 'iClass.php';
use ic\iClass;
iClass::callMeFromClass();