My current code is as following:
namespace Libraries;
class_alias('Libraries\ORM', 'ORM');
class ORM
{
public function __construct() {}
static public function someMethod()
{
// do something
}
}
I thought I could shortcut the namespace as you can see above, so I only needed to call the ORM::someMethod(); instead of \Libraries\ORM::someMethod();
(I am using the ORM class in another namespace, lets says 'Project')
Is this possible or what is the right solution?
I know that I could store the class in a global namespace, but then I still need to use the global slash like: \ORM::someMethod();.
Thanks!
Simply alias the classname when you are importing it:
namespace SomethingEntirelyDifferent;
use Libraries\ORM as ORM;
ORM::someMethod();
Related
I'm trying to understand how to use USE in base and extended classes. I have searched around but I don't think I have the correct terminology.
Let's say my base class looks like
namespace App\Classes;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\Legend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
class ExcelReport
{
public $spreadsheet;
public function __construct()
{
$this->spreadsheet = null;
}
}
and then I extended that class
namespace App\Classes;
class MonthlyExcelReport extends ExcelReport
{
public $id;
public function __construct(int $id)
{
parent::__construct();
$this->id = $id;
}
public function build()
{
$reader = IOFactory::createReader('Xlsx');
}
}
What do I have to do to get the call to IOFactory in the extended class to recognize that use PhpOffice\PhpSpreadsheet\IOFactory; is present in the base class?
I currently get this error Class 'App\Classes\Gap\IOFactory' not found and I don't want to have to repeat all of those use statements in the extended class.
TL;DR;
Namespace is there to allow you to have two classes named the same, but in a different namespace.
Imagine the class Animal\Bear\Claw and Machinery\Compactor\Claw, are things possible with namespaces, when we needed ugly class names like Animal_Bear_Claw and Machinery_Compactor_Claw before the introduction of namespaces in PHP.
Now when you instanciate or use those classes, you don't want to allways have to go in the extends and say
new \Animal\Bear\Claw();
You want to be able to say: "I am in the context of an Animal Factory Pattern and will basically act upon the classes under the Animal namespace, not the Machinery's ones"
So you go:
use Animal\Bear\Claw;
new Claw();
Or
use Animal\Bear;
new Bear\Claw();
Or even, with aliases
use Animal\Bear as MyTeddyBear;
new MyTeddyBear\Claw();
And so, inheriting another class from another containing uses, just does nothing, you'll have to repeat your uses, maybe simplifying them, and most likely, not add uses for class you actually do not use in the said class (did you know that good IDE does prompt you about unused use statement present in your classes and help you add the good ones to your use statements?).
The use statement are not like an include like you seems to believe it.
It is just there to say: "thanks to namespaces, I can have multiple classes with the same name, now the class I want to use is actually under the namespace defined by use"
You are not forced to state a full namespace either in your use statements.
For example:
namespace App\Classes;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class MonthlyExcelReport extends ExcelReport
{
public function __construct()
{
$reader = IOFactory::createReader('Xlsx');
$workSheet = new Worksheet();
}
}
Could be shortened this way:
namespace App\Classes;
use PhpOffice\PhpSpreadsheet; // This means "all the classes that I am going to use, if not in the same namespace as the current class (App\Classes) would come from the namespace PhpOffice\PhpSpreadsheet"
class MonthlyExcelReport extends ExcelReport
{
public function __construct()
{
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
$workSheet = new PhpSpreadsheet\Worksheet\Worksheet();
}
}
Further reading:
https://www.php.net/manual/en/language.namespaces.faq.php
https://www.php.net/manual/en/language.namespaces.rules.php
Use operator is used to "include" a class.
If you don't use "Use" operator, than you can include as "full path".
In your case:
$reader = IOFactory::createReader('Xlsx');
Should be:
$reader = PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
Normally I have a question about something not working, now I have a question about something that IS working, I am just confused as to why. This is the structure that I have in Laravel:
ExampleController
use App\Http\Traits\Trait1;
use App\Http\Traits\Trait2;
ExampleController extends Controller {
use Trait1, Trait2;
public function index()
{
// I can use methods from Trait1 and Trait2 here, works fine
}
}
Trait1
namespace App\Http\Traits;
trait Trait1 {
exampleMethodTrait1()
{
}
}
Trait2
namespace App\Http\Traits;
trait Trait2 {
$test = $this->exampleMethodTrait1();
}
Calling a method defined in Trait1 from Trait2 actually works, while I have not added use App\Http\Traits\Trait1; in Trait2. Is that because they are both loaded in the controller?
Okay, Let me put same code and explain you why it is working.
Trait1
<?php
namespace App\Http\Traits;
trait Trait1 {
public function exampleMethodTrait1()
{
echo 'okay';
}
}
?>
Trait 2
<?php
namespace App\Http\Traits;
trait Trait2 {
public function bar() {
var_dump(get_class($this));
$test = $this->exampleMethodTrait1();
}
}
?>
MyController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Traits\Trait1;
use App\Http\Traits\Trait2;
class MyController extends Controller
{
use Trait1, Trait2;
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$this->bar();
}
}
Now, if you will notice in Trait 2, var_dump(get_class($this)); $this is instance of MyController and not instance of trait 2, that is how it is working and it is expected behavior.
Now if you want to know if you can use one trait in side another
YES
You can do like
TaraitA
Trait A {
}
TraitB
Trait B {
use A;
}
And it will work fine.
Yes, they are both loaded in your controller as a part of it therefore they have access between them also controller methods
See the example 4
https://www.php.net/manual/en/language.oop5.traits.php
Regards
I think your confusion comes from believing that the $this inside a trait corresponds to the trait itself. But it is not.
Traits are nothing by themselves: they exists only in the context of a real class, as a helper to copy-paste methods around but not visually polluting your actual classes.
The $this you use to call exampleMethodTrait1 is not an instance of Trait2 (nor Trait1) but an instance of ExampleController, that has copied the methods over from the traits.
This doesn't happen only with traits, though, but also with parent classes in the hierarchy:
Example
abstract class Base {} // First level of inheritance
class Building extends Base {} // Second level of inheritance
class House extends Building {} // Last level of inheritance
$this (and static) always corresponds to an instance of the most concrete class of the hierarchy (the last level of inheritance).
self instead refers to the actual class instance (the same level of inheritance where the method is defined). Still never a trait, they cannot be instantiated by themselves.
the traits are not part of the hierarchy, but blindly pasted where you use them.
I'm currently working on a PHP trait thay will help me to reuse code in some class controllers that I have using Laravel framework.
I wanted to make the trait methods as dynamic as I could but when trying to access to a class that my parent class imported, I get a Class not found exception.
My class controller is as follows:
namespace App\Http\Controllers\Admin;
use App\Models\ {
Curso,
Leccion,
Diapositiva,
ImagenDiapositiva
};
use App\Traits\TestTrait;
class DiapositivasController extends Controller{
use TestTrait;
public function addRecord(Request $request){
$request->class_name = 'ImagenDiapositiva';
$this->addImage($request);
}
}
My Trait:
namespace App\Traits;
trait TestTrait{
public function addImage($request){
$class_name = $request->class_name;
$diapositiva = new $class_name;
//extra code
}
}
So my doubt is, do I have to include the model classes I want to use inside my Trait again or am I doing something else wrong?
if you use new with a variable class name, you have to use the fully qualified class name. I'm guessing new $class_name is the root cause of the issue here, since $class_name would have to be something like: 'App\Models\ImagenDiapositiva' or whatever the full namespace is. Just have to change the call $request->class_name = 'ImagenDiapositiva'; to reflect the full name of the class.
I have created a common class in app/Classes/Common.php
but whenever i try to access a model in a class function.
$new_booking_request = BookingRequest::where('host_id','=',Auth::id())
I am getting this error
Class 'App\Models\BookingRequest' not found
Even other classes like Auth, URL and Cookie are not working.
Is there a way to bring all classes in my Common class scope?
You get this issue when your namespace is wrong you or you forgot to namespace.
Since common.php is inside App/Classes, inside Common.php do somethng like this:
<?php namespace App\Classes;
use View, Auth, URL;
class Common {
//class methods
}
Also ensure your model class has the correct namespace, if BookingRequest.php is located inside App\Models then inside BookingRequest.php do this:
<?php namespace App\Models;
BookingRequest extends \Eloquent {
//other definitions
}
Then if you wish to use BookingRequest.php outside its namespace or in another namespace like so:
<?php namespace App\Classes;
use App\Models\BookingRequest;
use View, Auth, URL;
class Common {
//class methods
}
In Laravel 5 everything is namespaced, make sure your class has a proper namespace and that you are calling it using that same namespace you specified.
To include classes in another class make sure that you use the use keyword to import the necessary classes on top of your class definition. Also you can call the class globally with the \. Ex: \Auth, \URL and \Cookie
For the namespace in L5 here is a quick example:
<?php namespace App\Models;
class BookingRequest {
// class definition
}
then when trying to call that class, either call the full namespace path of the function, or include the function.
<?php
class HomeController extends Controller {
public function index()
{
$newBookingRequest = App\Models\BookingRequest::where('host_id','=',Auth::id());
}
}
OR
<?php namespace App\Controllers;
use App\Models\BookingRequest; // Include the class
class HomeController extends Controller {
public function index()
{
$newBookingRequest = BookingRequest::where('host_id','=',Auth::id());
}
}
PS:
Please use camelCase when defining class attributes and methods as this helps for a better code-styling and naming conventions when using the L5 framework.
Have encountered an issue I can't seem to figure out now by myself.
Using Symfony autoload module.
Here's my factory:
namespace Core\Factories;
use \Core\Gateway;
class DatabaseAccessFactory {
// Define client type
const DEF = 'mongo';
public function createObject($type) {
switch($type) {
case self::DEF:
return new MongoClientGateway();
break;
default:
return false;
}
}
}
Example of /Core/Gateway/MongoClientGateway.php
<? namespace Core\Gateway;
class MongoClientGateway implements MongoDbGateway {
public function setUp(){
}
public function query(){
}
public function save(){
}
}
So, basically I'm using "use" keyword to load gateway namespace into my current one, and then I try to instantiate a class that is under \Core\Gateway namespace, but it says class is not found. Am I missing something?
You need to specifcy the class as well
use Core\Gateway\MongoClientGateway
or access the class with the namespace you used
new Gateway\MongoClientGateway
Btw, there's no need for the first "\" in use \Core\Gateway
It's use Foo\Bar, without leading backslash.
use Foo\Bar does not mean that every Class implicitly resolves to Foo\Bar\Class now. use Foo\Bar is shorthand for use Foo\Bar as Bar, so you can reference the namespace Foo\Bar using merely Bar. use is not "importing a namespace", it's aliasing a namespace to a shorter name.
Therefore you need to write Gateway\MongoClientGateway, or use Core\Gateway\MongoClientGateway explicitly if you want to be able to write just MongoClientGateway.
you used "use" wrong.
waht "use" does, is to tell your code where class comes from.
sample code:
use \my\namespace\className
new ClassName();
this will make the className accassible without a namespace.