File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed
PHP A to Z OOPS/Namespaces Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ //Both of these files contain classes with the same name, which we cannot include.
3+ //Now, we will use namespaces so that we can include both classes with the same name and use their functions.
4+
5+ require "product.php " ;
6+ require "testing.php " ;
7+
8+ function wow ()
9+ {
10+ echo "Wow From Index file \n" ;
11+ }
12+
13+ #To call a function of a particular class,
14+ //$obj=new pro\Product();
15+ //$obj1=new test\Product();
16+
17+
18+ // $obj->Myfunc();
19+ // $obj1->Myfunc();
20+
21+ #Call Same name function from different-2 namespaces
22+ // wow();
23+ // pro\wow();
24+ // test\wow();
25+
26+ //we can make object of "\test\Product" and call its methods
27+ //$obj=new pro\Product();
28+
29+
30+
31+ #Long Namespaces Usage:-
32+ //$obj=new pro\version_1\command\myname3\Product();
33+
34+ #If you need to create a large number of objects with long names, you can use this approach.
35+ use pro \version_1 \command \myname3 as mycmd ;
36+
37+ $ obj = new mycmd \Product ();
Original file line number Diff line number Diff line change 1+ <?php
2+ //This class is in namespace.
3+ namespace pro \version_1 \command \myname3 ; //First way to declare namespace
4+
5+ class Product
6+ {
7+ public function Myfunc ()
8+ {
9+ echo "Message:- Product Class \n" ;
10+ }
11+
12+ public function __construct ()
13+ {
14+
15+ $ test = new \test \Product (); //we can make object of "\test\Product" and call its methods
16+
17+ $ test ->Myfunc ();
18+ }
19+ }
20+
21+ function wow ()
22+ {
23+ echo "Wow From product file \n" ;
24+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace test { //Second way to declare namespace
4+
5+ class Product
6+ {
7+ public function Myfunc ()
8+ {
9+ echo "Message:- Testing Class \n" ;
10+ }
11+ }
12+
13+ function wow ()
14+ {
15+ echo "Wow From Testing file \n" ;
16+ }
17+ }
You can’t perform that action at this time.
0 commit comments