Skip to content

Commit 3fca83b

Browse files
author
navjotsinghprince
committed
The PHP Professional
1 parent f37314b commit 3fca83b

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}

0 commit comments

Comments
 (0)