Namespace, use reach file one folder up - php

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.

Related

How do I resolve missing class function in codeigniter?

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).

How to use namespaces in PHP

I am trying to learn the basic concept of namespace and trying to implement but it always gives an error
The following is the method that I have used for namespaces
First I create a folder name namespace inside the folder name www, then I create two files
functions.php
index.php
I am writing the following code in functions.php
namespace MyProject;
function connect(){
echo 'connecting';
}
Code in index.php
echo \MyProject\connect();
Now I am running the file it always gives an error - by why? Can anyone help me out?
I don't see you mentioning including the functions file.
require('functions.php');
echo MyProject\connect();
Namespaces aren't substitutes for includes; the "magic" you see in frameworks or other structured code is likely an autoloader which takes care of converting namespaces to a folder/file structure and then including the file needed.

Using php namespaces properly

I am trying to figure out how to use the namespace in php. I have been reading about how to use it and for some reason I can not get it to work. I have two files one which I have stored in Applications/Database/Classes file name is DatabaseConnection.php and the other in the root directory called DB.phpinside the DatabaseConnection.php file I have the following code:
<?php
function hello()
{
echo "hello";
}
?>
This is the DB.php file contents:
<?php
namespace Applications\Database\Classes;
ini_set('display_errors', true);
hello();
?>
Maybe I am completely missing how to use it properly but if I set a namespace is that the same as using include or require? I might be completely misunderstanding how to use it. I am new to OOP and have never heard of namespaces until I started trying to learn OOP? Can someone point out what I did wrong.
Namespaces are for organizing your code in so that you can divide components up and help with the readability. For example if I have a class Pittbull and another Dashund I can place them into a namespace like so for organization:
Animals.Dogs.Pittbull
Animals.Dogs.Dashund
This also helps with potential collisions like the below:
Animals.Dogs.Misc
Animals.Cats.Misc
The Misc class exists twice in this instance, but instead of there being a conflict of which Misc to use, you can use the same class name for both classes (and have different properties and methods inside of them) and not have a conflict of which one you want to use.
The require keyword is a completely different concept and is used to load actual files into the executing script.
Instruction how to use autoloading in PHP (PSR-0):
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

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.

PHP dynamically generate namespace based on folder structure

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.

Categories