I have classes in two different namespaces, for example:
Controller is in \Core,
Index is in \Public
In my index.php, I have a class_alias for all of the \Core classes, so you can call them directly: $controller = new Controller();. This works without issue.
My problem is when I try to extend the class. Since Index & Controller are in different namespaces, it tries to find Controller in the \Public namespace so this doesn't work:
<?php
namespace Panel\Pub;
class Index extends Controller {
Is there any way around this so I can use the class alias in the extends function? I know I can use \Core\Controller and it will work, but I'm trying to use aliases to make core functions more easily accessible.
Edit: Found one workaround
After doing some more testing, I found that using \ in front of the alias in the extend seems to work. Not as ideal as no \ but currently the best solution results in:
class Index extends \Controller { }
Still looking for other advice on a work around or different method of extending controller.
Thanks!
When you're using a namespace, you use the full path in your namespace
class Index extends \Public\Controller { }
\ represents the root of your namespace.
Another way to do this is to use the use keyword
namespace Panel\Pub;
use \Public\Controller as Controller
class Index extends Controller { }
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'm using Phalcon PHP with Multi module application. I'm using namespace in my project but I'm searching for something to use theses namespace.
For example, in my view folder I'm using the models folder and in my controller I use the models folder too. But I'm using lot of class models to do a Phalcon find or findFirst. And the only way than I found to make this multi apps working, it's to define the namespace used to import the class like this :
use Apps\Common\Models\Users;
use Apps\Common\Models\Customers;
use Apps\Common\Models\Agents;
...
And I have 50 models like this in my apps... I don't want to define them in all my controller and all my view to make it work.
Do you have a solutions for that ?
Thanks.
If I understood correctly, you can omit the namespace declaration on top of your controller file:
use Models\News;
class NewsController extends BaseController
{
public function indexAction()
{
// With Use above
$obj = new News();
// Without Use above (full namespace path)
$obj = new \Models\News();
}
}
I encountered the topic called 'namespace' in php after I started working with Laravel. While trying to understand namespace I found that to extend a class under a namespace, I need to include that class in my current page. Like the following:
directory '..\teacher\Teacher.php'
namespace Teacher;
class Teacher{
public $headTeacher='mr X';
}
to extend the calss i need to include that page as well as use the namespace
directory '..\studnet\student.php'
use \Teacher\Teacher; //use the namespace
include('../teacher/Teacher.php'); // include the page
class mathTeacher extends Teacher{
public function headTeacherName(){
echo $this->headTeacher;
}
}
$student=new mathTeacher();
$student->headTeacherName();
I am wondering how Laravel only use namespace to include classes. Like if I create a controller called 'userController'. The structure of the page is
namespace App\Http\Controllers;
class userController extends Controller{
}
They never included the php page which holds the 'controller' class. But they were able to extend it somehow. Also I can use "View" ,"Auth" just by using the use View or use Auth command. How is it done? How can I implement the same with the code I have provided? Thanks in advance.
Laravel uses composer.php for autoloading the classes. All classes in the autoload directory will be pre loaded. So you can just use the namespace and consume anywhere across the application.
Learn more about composer, composer config can be found on composer.json in your root path for the application
I am using laravel 4.2.
Lets say there is such class:
class BShopsController extends ShopsController
To fix this, I try to use name space lets say this:
namespace app\controllers;
and then it does not find ShopsController
so I add
use \ShopsController;
Then I get error:
Class BShopsController does not exist
What namespace should I use first of all so it would not break anything?
Edit:
BShopsController and ShopsController are in folder Shops
As your files are inside the Shops folder and I believe that the Shops folder is inside the app folder you should namespace your class the following way.
<?php namespace Shops;
class BShopsController extends ShopsController{}
Similarly,
<?php namespace Shops;
class ShopsController{}
So with the help of Shhetri and this Using namespaces in Laravel 4
I did this way:
namespace App\Controllers\Shops;
class BShopsController extends ShopsController{}
Also in routes.php then need to change to this:
Route::controller('shops', 'App\Controllers\Shops\ShopsController');
And where calling action() method - also need to use namespace.
Also needed to run
composer dump-autoload -o
otherwise were errors.
Also in ShopsContrller needed to to this:
use \App\Controllers\BaseController;
Because Shops controller was in another namespace than BaseController and cannot find it. But is extending from BaseController, so need it.
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.