PHP dynamically generate namespace based on folder structure - php

I have a folder structure like so:
index.php
app/
controllers/
folder1/
class.php
There is no namespace definition in class.php. Would it be possible to put class.php in a namespace generated from folder structure relative to index? So it would be loaded like:
new \app\controllers\folder1\classInFile();
Or is there no way to dynamically create namespaces?

A file containing a namespace must declare the namespace at the top of the file before any other code.
http://php.net/manual/en/language.namespaces.definition.php
I.e., you cannot execute any code before the namespace declaration, and afterwards it's too late. Notwithstanding introspective runtime hacks: no, it's not possible. Even if it was, it would depend on runtime information, like what folder the code is executed/included from. The namespace could be var\www\myproject\foo\bar\baz or just foo\bar\baz. How are you going to determine that? That's getting messy.
Really, just make it explicit, even if that means typing a little more. The namespace is part of the class's name. You should not generate names dynamically at runtime.

You shouldn't be dynamically generating namespaces, the namespace cannot be set and read at compile time.
In any case, the namespace is provided/defined before any code is executed.

The namespace needs to be defined inside of class.php if you can create the PHP source file dynamically, you can create the namespace dynamically.

Related

I am having a error in namespace and "use" statement in php [duplicate]

This is what I have at hand:
//Person.php
namespace Entity;
class Person{
}
User file:
//User.php
use Entity\Person;
$person = new Person;
Here, it fails if I don't include the Person.php file. If I include it, the everything works fine. Do I absolutely require to include the file even when using namespaces? If at all we need to include/require files, then how can namespaces be effectively used? Also, can we maintain folder structure by nesting namespaces?
The answer to your question is "yes and no".
Indeed the code implementing class Person has to be included, otherwise the class is not defined and cannot be used. Where should the definition come from, when the code is not included? The php interpreter cannot guess the classes implementation. That is the same in all programming languages, by the way.
However there is something called Autoloading in php. It allows to automatically include certain files. The mechanism is based on a mapping of class names to file names. So in the end it boils down to php searching through a folder structure to find a file whos name suggests that it implements a class currently required in the code it executes.
But don't get this wrong: that still means the file has to be included. The only difference is: the including is done automatically, so without you specifying an explicit include or require statement.
Yes, you need to include every file.
A very good example can be found here on effective usage of namespaces.
With PSR-0 autoloading, the namespace has to be the same as the folder in which the class is, file the filename has to be the same as the classname. This gives you very simple and effective autoloading with composer for example.

Am I using namespaces, autoload, and aliasing correctly?

I've been reading a lot of posts on StackOverflow but I'm not really sure I'm using namespaces, autoloading, and aliasing correctly. This is functioning fine, but I'm not sure I'm properly using these concepts. I've listed some reasons why I think this setup is incorrect at the bottom of the post.
Imagine the following directory structure:
\public_html
- index.php
\Classes
\A
- One.php
\B
- Two.php
One.php is structured like:
<?php
namespace Classes\A;
class A { ....
Two.php is structured like:
<?php
namespace Classes\B;
class B { ....
Then, in index.php I do something like:
<?php
use Classes\A\One as One;
use Classes\B\Two as Two;
spl_autoload_register(function ($className) {
...
});
... ?>
So, a couple things that bug me about this:
If I am doing aliasing (the "use" statements) I still need to list out all of the files. Aren't we trying to avoid doing this by using autoload?
If I want to use internal classes, I need to add a line such as "use \mysqli;" into the class that uses this and do things like "new \mysqli()". Seems kind of messy?
If a class extends a class from another namespace (say Two.php extends One.php for example) then I need to include "use \Classes\A\One as One;" in One.php which seems to be what we want to avoid in the first place
You don't have to reference all of your namespaced classes via a use statement. You can use partial namespaces.
use Classes\A;
new A\One();
So you can see if you had another class in the A namespace it could be instantiated with
new A\Three();
There are many things in the global namespace, you don't need to define them with use. You can just call them \mysqli(). Yes it's a bit unsightly but it allows you to make a function called mysqli in your own code without worrying about collisions with globally namespaced functions.
I'm not sure I follow you on this one. You don't need to do a use in the base class which references itself.
Ultimately it kinda seems like you view use almost like include when they are very different, use, in this context, is a convenience thing so you don't have to type out full namespaces every time.
Your Autoloader also doesn't need to know about your namespaces. The autoloader just tells PHP how to load a class. So if it sees a class come in with the name Classes\A\One you can make it look in the directory /Classes/A for a file called One.php and include it. PHP will make sure that the class is allocated in the proper namespace. The autoloader is just a convenience thing to keep you from having to do includes and requires in each of your PHP files.

Is it required to include the namespace file?

This is what I have at hand:
//Person.php
namespace Entity;
class Person{
}
User file:
//User.php
use Entity\Person;
$person = new Person;
Here, it fails if I don't include the Person.php file. If I include it, the everything works fine. Do I absolutely require to include the file even when using namespaces? If at all we need to include/require files, then how can namespaces be effectively used? Also, can we maintain folder structure by nesting namespaces?
The answer to your question is "yes and no".
Indeed the code implementing class Person has to be included, otherwise the class is not defined and cannot be used. Where should the definition come from, when the code is not included? The php interpreter cannot guess the classes implementation. That is the same in all programming languages, by the way.
However there is something called Autoloading in php. It allows to automatically include certain files. The mechanism is based on a mapping of class names to file names. So in the end it boils down to php searching through a folder structure to find a file whos name suggests that it implements a class currently required in the code it executes.
But don't get this wrong: that still means the file has to be included. The only difference is: the including is done automatically, so without you specifying an explicit include or require statement.
Yes, you need to include every file.
A very good example can be found here on effective usage of namespaces.
With PSR-0 autoloading, the namespace has to be the same as the folder in which the class is, file the filename has to be the same as the classname. This gives you very simple and effective autoloading with composer for example.

PHP namespace in relation to file structure

I have yet to find a clear answer on how PHP namespaces work in relation to the file strucutre
If I have some code say
<?PHP
namespace Go/Project
Use Foo/Bar/Dog
class cat extends dog
{
.....
}
?>
What does the file structure for something like this look like and do I need to reflect what I am doing in the namspaces and the imports in the file structure or do I need to add some include()s in there.
It depends on your autoloader.
Some frameworks use standard of PSR-0, some not use. You can write your application without any autoloader and write namespaces independent of your files structure.
For example you can declare namespace Acme/Foo/Bar/Do/For/Me/Some/Thing in index.php of your DocumentRoot and all classes declared in this file will be stored under this namespace.
But if you want to not write in every file require statements for your files you may want to write your autoloader function and make dependence between files structure and namespaces structure.

Namespace, use reach file one folder up

Im trying to reach a class file where I have defined namespace in the top with the following:
File: config.php
namespace Config;
class Config
{
// Stuffs
}
The file structure is like this
public_html
- admin
-- header.php
-- footer.php
-- file-trying-to-reach-config.php
- config.php
Im trying to reach the config.php -file from the file-trying-to-reach-config.php. In the top of file-trying-to-reach-config.php Im using:
use \Config;
But I have no clue how I, with use move up on folder to reach config.php. Googled and Stacked but didn't find anything about it. Have I misunderstood the concept with namespace and use?
How do I reach config.php with use from file-trying-to-reach-config.php?
The Use operator is used to alias namespaces in PHP, as described in this documentation. This essentially gives you the ability to create short names for classes when you have complex namespaces.
You need to either include or require the config.php file into where you want to call it. e.g.
<?php
include_once('../config.php');
$config = new \Config\Config();
For a more advanced way to handle this, you can look into Autoloading which will do the include bit automagically.

Categories