1 / 11

How to Use Namespace in PHP

Namespaces are the core part of ROR(Ruby on Rails). Its new feature in PHP and have released before some years round June 2009 in PHP.

dynaaddison
Download Presentation

How to Use Namespace in PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NameSpace Created by www.creativedev.in

  2. About Namespace • Before some days, I don’t have idea about Namespaces in PHP and I have searched a lot for this and i didn’t understood it and finally lots of headache, I am clear with namespace now and i want to share my knowledge with you because if you are using PHP 5.3 , you have to know about Namespaces and Exceptions. • Namespaces are the core part of ROR(Ruby on Rails). Its new feature in PHP and have released before some years round June 2009 in PHP a. We can say Namespaces are one of the most significant features in PHP 5.3.0 and if we try to use if in older version of PHP, error message would be appear. • Namespaces are common concept of OOP because its a simply collection of classes, functions and objects. We can put any of the PHP code in namespace but main purpose of namespace is to avoid naming collision of classes, functions and variables.

  3. Defination • Namespaces are the way to encapsulate classes, functions or constants to a specific library or area to solve certain conflicts that can arise when two different classes are given the same name.With the use of namespaces ,we can avoid the naming conflict between one or more classes.when you add code under a namespaces, it would be unique from other’s code even if they have the same entity name.

  4. Where can You Use Namespaces? • You probably don’t need namespaces for small applications or applications with only one developer, or applications without frameworks of third party code but if your application have large set of classes libraries and functions within class and If your application has more than one developer, or you start to use third-party libraries,its good to use namespaces to keep your code clean, and prevent name conflicts in your code, that are difficult to find and fix.

  5. NameSpace SYNTAX namespace {ClassName, FunctionName}; To declare a namespace ,keyword ‘namespace’ is used and to use namespace,Scope Resolution Operator (::) or keyword USED is used. We should declare the namespace as above and we can also create nested namespaces using a backslash() between multiple names of namespace.

  6. How Do You Use Namespaces? First Create one demo.php file and add below code: namespace test; class Testclass { function Welcome() { echo 'Welcome'; } }

  7. And next create Test.php file which can contain below code: include "demo.php"; $test = new testTestclass(); $test->Welcome(); // Prints 'Welcome‘ Above Example show you the class referenced with the Namespace and after that you can use the Scope Resolution Operator and instantiate the “Test” class.

  8. You can use more than one namespaces in single file like namespace first; class FirstClass(){ // Code } namespace second; class SecondClass(){ // Code }

  9. How to define Nested namespaces in the same file? • SYNTAX namespace MyFirstNamespacesubnamespace;

  10. “Use” Namespace • Alright, we have declared namespace but how to use them its a main thing of it so let’s see how to use nested namespaces. • If we want to use namespace in other files, we can use it with “USE” keyword. so USE is most and important thing for namespaces. • SYNTAX use MyFirstNamespace; Now with the use of “USE” keyword any constant, classes, or functions in the file will now be part of “MyFirstNamespace”.

  11. Thank You  To get more update visit www.Creativedev.in

More Related