Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use default php classes in my namespace?

Tags:

namespaces

php

I am using namespaces to resolve class name conflicts in two of the SDKs I am using in my project

I have declared a name space in one of the file like

namespace temp;

class abc extends stdClass
{
  // my class def
} 

when i am hitting this code i get error says temp/stdClass not found, so I need to use all default php structures, interfaces like iterators etc. so how can I import the default namespace of php or do any another setting I am missing?

like image 353
Rupesh Patel Avatar asked Sep 03 '12 12:09

Rupesh Patel


People also ask

What is class namespace PHP?

A namespace is a hierarchically labeled code block holding a regular PHP code. A namespace can contain valid PHP code. Namespace affects following types of code: classes (including abstracts and traits), interfaces, functions, and constants. Namespaces are declared using the namespace keyword.

What is the best approach for working with classes and namespace in PHP?

To address this problem, namespaces were introduced in PHP as of PHP 5.3. The best way to understand namespaces is by analogy to the directory structure concept in a filesystem. The directory which is used to group related files serves the purpose of a namespace.

How does PHP namespace work?

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions: Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.

Can I use two namespace in PHP?

Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.


1 Answers

Add \ before any global class, function and constant (stdClass in your example). Here is more information how to use namespaces.

like image 180
Leri Avatar answered Sep 28 '22 07:09

Leri