
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Accessing Global classes
Introduction
When PHP parser encounters an unqulified identifier such as class or function name, it resolves to current namespace. Therefore, to access PHP's predefined classes, they must be referred to by their fully qualified name by prefixing \.
Using built-in class
In following example, a new class uses predefined stdClass as base class. We refer it by prefixing \ to specify global class
Example
<? namespace testspace; class testclass extends \stdClass{ // } $obj=new testclass(); $obj->name="Raju"; echo $obj->name; ?>
Included files will default to the global namespace. Hence, to refer to a class from included file, it must be prefixed with \
Example
#test1.php <?php class myclass{ function hello(){ echo "Hello World
";} } ?>
This file is included in another PHP script and its class is referred with \
when this file is included in another namespace
Example
#test2.php <?php include 'test1.php'; class testclass extends \myclass{ function hello(){ echo "Hello PHP
"; } } $obj1=new \myclass(); $obj1->hello(); $obj2=new testclass(); $obj2->hello(); ?>
Output
This will print following output
Hello World Hello PHP
- Related Articles
- PHP Global space
- PHP Anonymous classes
- PHP Autoloading Classes
- Anonymous classes in PHP 7?
- How to declare a global variable in PHP?
- Pseudo-classes and CSS Classes
- Global Recession
- Pseudo-classes and all CSS Classes
- Accessing SAP HANA Information Composer
- Accessing Array Elements in Perl
- Accessing Hash Elements in Perl
- JavaScript global Property
- Explain Global Warming?
- Define Global Warming.
- Accessing table data from SAP system

Advertisements