PHP namespaces trouble - php

I just wanna use a namespace in another file, to use the class in it, but im too retarded to do this...
first file:
namespace fun;
use fun\kjr\trouble;
$trouble = new trouble('http://someURL');
second file:
namespace fun\kjr;
class trouble { ... }
This is the error i get:
Error: Fatal error: Class 'fun\kjr\trouble' not found in D:\wamp\www\fun\index.php on line 8
Where did I declare a wrong namespace? Oo
Greetings

I had to include my class aswell.
use namespace
only makes the class available in the actual context.
It does NOT include it, so you can cause it.
use
include_once('your.file')
to do that!
Thanks #Michael Berkowski for helping.

The problem (from what I can see) is you haven't specified an autoloader (e.g. with spl_autoload_register()) or haven't included the file in your code. The use statement will only find a class for you if you have an autoloader in place, otherwise you must include your code ahead of time with include/include_once or require/require once.
I would start with making an autoloader and registering it and then it may be wise to map your namespaces to directory structure as specified by PSR-0.

Related

Declare only once the Use "Namespace" initialization and all files got the same namepace for an object

I got a simple question for you hopefully. Can it be possible that I just initialize the namespace for a class once in my "main file" and all other objects or classes got the same reference of it ?
For example Index.php:
require_once 'init.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
and now I got a class not in the same file with this kind of code. Controller.php:
function programmAction($id) {
$programm = $this->model->getProgrammById($id);
$html = $this->renderTemplate($this->tempProgramm,['programm' => $programm]);
return new Response($html);
}
Normally when I don't add the use lines as well to this file I would get an error message that the class Response was not found.
Only if you include all other classes in the same file. Refer to this note from the manual:
Note:
Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.
Please don't include all of your classes in one file.
It may seem like a pain to retype the use statements in each and every class file, but really it will help you when you go back and refer to that file later. Right at the top is a list of classes that your class depends on, and could be helpful information during a refactoring or code reorganization.

Facebook PHP SDK 4.0: using classes in subsites

I think this is very simple for many of you, but in the moment I got stuck with this. I have the following part of code:
header.php
include "facebook/autoload.php";
use Facebook\FacebookRedirectLoginHelper;
test.php
include "header.php";
$helper = new FacebookRedirectLoginHelper($redirect_url);
Why do I always get this error:
Fatal error: Class 'FacebookRedirectLoginHelper' not found in test.php on line
I thought when I include a PHP file, classes can also be used. But in this case not, why? I think I do not understand how this autoload and use works, so I would be happy for some explanation.
PHP does not inherit the namespaces nor the use statements of included/required files. This is intentional as otherwise if you include 2 files using a class aliased the same way you will get errors and you might not need all those classes in firs place.
If a class requires a namespace it has to have use statement defined with the full namespace to the particular class they need. Except in the cases where there might be aliasing. For example if you have:
// file1.php
use \My\Cool\LogWriter as Writer;
and
// file2.php
use \My\Cool\FileWriter as Writer;
Now both classes are accessible as Writer.
// test.php
require 'file1.php';
require 'file2.php';
In which case if you don't declare which class from which space you want this will give nasty error that class Writer is defined, which is true, but it is also true that the two classes are 2 separate ones.
For more information on namespaces in PHP5 see (http://php.net/manual/en/language.namespaces.php).
As a side note:
Every file, if not namespace declaration is provided is considered in the global namespace.
If a use is without leading slash the namespace might be considered relative to the current. (unsure but I think it depends on the autoloader?) (Reference here: https://stackoverflow.com/a/4879615/1747193)

How can I call a function in a php class?

This is a sample code:
sample code
I want to call it in another page:
include 'root of class file';
$r = new ImagineResizer();
I got this error:
Fatal error: Class 'ImagineResizer' not found in C:\WampDeveloper\Websites\example.com\webroot\images.php on line 13
Also call the function:
$r->resize('c:/test1.jpg', 'c:/test2.jpg');
As seen in your sample code the class is located in another namespace :
<?php
namespace Acme\MyBundle\Service;
use Symfony\Component\HttpFoundation\File\File;
use Imagine\Image\ImagineInterface;
use Imagine\Image\BoxInterface;
use Imagine\Image\Point;
use Imagine\Image\Box;
class ImagineResizer {
//-- rest of code
}
To use a class in another namespace you need to point out where the file is :
First include the class (manual or with autoloading)
Then u can create an instance in 2 ways. First way with the use-keyword
use Acme\MyBundle\Service\ImageResizer;
$object = new ImageResizer();
Or point to the class absolute :
$object = new \Acme\MyBundle\Service\ImageResizer();
Hopefully, this will help you out some:
Make sure you include the actual file - not just the folder where it lies.
Make sure that the file you're calling the class from uses the same namespace as your class file. If it doesn't, you have to call the class using the full namespace.
Profit.
The namespaces really had my patience go for a spin when I started using them, but once you're used to it it's not too hard. I would recommend using an autoloader though. It's a bit of a hassle to set up, but once it's done it helps out a bunch.
Namespaces: http://php.net/manual/en/language.namespaces.php
Autoloader: http://php.net/manual/en/function.spl-autoload-register.php

In php namespaces do I need to require each file

If I'm using namespace and for example I have the following directory tree
Test/someclass.php
Test/someotherclass.php
can I simply do
use \Test\someotherclass
or I need first to do
require 'Test/someotherclass.php'
and then I can actually use that class
because currently when I do that I get the following error
Fatal error: Class 'Test\someOtherClass' not found in
C:\xampp\htdocs\test\Test\someClass.php on line 10
As said in the comments the namespace and the use statement don't load anything by default.
But you can use an autoloader based on namespace. You have a standard called psr-0 for autoload.

How to resolve a namespace on php?

I'm doing the first test on php5. More specifically I'm trying to work with namespaces but it seems that php is not resolving the namespace. Here the test I've done:
file 1: test.php
<?
namespace \first_test;
class Test {
function test_function(){
print "works";
}
}
?>
file 2: use_namespace.php
<?
use \first_test;
$a=new \first_test\Test();
$a->test_function();
?>
I get a fatal error:
Fatal error: Class 'first_test\Test' not found in
here_the_path_where use_namespace.php is.
Any help appreciated.
P.S. Both files are on the same directory, php 5.3.9 on windows.
You don't need the \ when declaring a namespace, you only need it when calling functions from it.
<?php
namespace first_test;
// ...
?>
Also, in your 2nd file, you need to include test.php, otherwise it doesn't know what first_test is.
In the 3rd-party library they most likely use autoloader, which automaticly does all the required includes. So if you dont want to manually write include, add your own autoloader
http://php.net/manual/en/language.oop5.autoload.php
It is a general aproach that you entry point contains autoloader, so all included files will use it automaticly. Description of coding standart for this can be found at https://www.php-fig.org/psr/psr-4/

Categories