OOP PHP | Namespaces in PHP #19

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back to earth series or PHP and in this episode we are going to learn about the named pieces in the PHP so before going ahead I have told you about the autoloading system and in this episode I have told you that we will going beyond the autoloading system if you have a large project then there will be some talk of what are the problem that will start our first discussion for why we need namespace ok so obviously you cannot create two files having same name in the folder ok but suppose you can create another file with another name but even that you cannot have same class name in a single directory ok then obviously you cannot do these things so this is not a big deal for a small project but if you are working on a big project or having a book on team then you have some problem because the name of the class will clashes will collide ok obviously you can create a new folder and put your class in text folder and you can use that word again it's having the problem because sometimes if you use third party package or library then they have the same name of the class which you also have then it will be again having the clashes or these user user classes are very common and every one I think has this class so that will be now a clash so if you use a user class then how did you define that that user class is from library package as from admin folder is from your faithfulness how do you many DT so the normal solution is using the underscore ok so simply use the class name having underscores so you have school underscore student underscore syllabus and risk for physics and it's called force this means this force class you have created is inside this school folder then inside student sub folder then sub sub folder of syllabus then sub sub sub folder of physics then you have this class then and obviously this will going to the endless system of creating a huge huge file named Edie oh my god how will you deal with this thing so now comes the real solution called mimesis so what is actually the name is base name is this is a virtual directory you create for your project it has no relation with the real folder structure you are following to manage your file you can have two files having the same class name but they can be used because you have created the different names and don't even just I will show everything in just a moment and this Nemesis system is introduced in PHP 5.3 and you just below the PHP tag and above everything okay so I have told you that namespace is a virtual directory structure for class in a global field but what is global space so let's see think about global space as a base folder of your project that means where you have put your main class then you can create first name space then second name and think about these two as a subfolder inside this global space and obviously you can create class a and B in this name is safe and class may n BN in this name then you can create a new sub first name space and sub first names this too and they also can have class name a class named B and similarly here yes this means you can see we have 1 2 & 3 class a & 1 2 & 3 plus B but because they are all at different sub spaces then they can be distinguished from one another if you are not convinced if you have some doubt just be with me I will show you what is it is because this namespace concept is little difficult to understand but I will make it easy for you okay so let's go to sublime words more here I have the namespace folder let's create a file called index dot PHP here PHP tag so here it is and let's create another file inside this names a folder called the first namespace so I will use F and F dot PHP and here I will create class called a ok as we have discussed ok and what this will do this will just have a constructor constructor which is just going to echo out I am at global space ok and let's use this file and echo just so I will just include this first name space then I will say object is equal to dollar new a ok the class name we have is a so let's go to Chrome and here I will go to name space and yeah I am at Google States that's very good no problem but suppose I will create another file called second name space SNS dot PHP and here I will say PHP tag and I will just copy everything from here and paste it here ok now I have two classes one is this a class inside F and F dot PHP second is inside SNS first name space ok so let's try to include that also once more second name strip go to Chrome reload and it says cannot declare class a because name is already in use because this class also have class name a so now what we have to do we have to use the name system because the name is clashing now what we can do we can use this second name space as we will provide it as name the space and I will say it as a second okay so simply is done and now if I go to Chrome reload the page it's not having clashes because we have not ever called this class a from second a mistake so how will the all the second namespace class a we just have to do one thing what is that we have to give here second the name is Pierre okay we have given here name Smith so think about this this second is just a virtual folder where you have put your class or even this file so now PHP will get this class inside this second folder okay second named folder okay go back to Chrome reload and it says I'm at global space again so because it again saying I'm at global space so I will say I am at second spit okay reload yeah I'm at second stage that means now I can access this file if I want to access this class name I will use the second key word which which is actually the name space of here okay so this actually is called the qualified class name so I will write qualified class name so that should be commented okay so this is qualified class name but we can do one thing we can even put our index inside this namespace so if I use name is space second here now I try to call let's see what happens here it's saying new error oh how can we resolve this so let's take a closer look here it says second second /a that means it's first going to this second because I have told you PHP that my index dot PHP is inside this second name space so it will firstly go to second name space then again we have given that we have to go to once more into the second name space okay so that's why it's given here so if I removed this now try to access yeah I am ex I can't spit this means because index dot PHP and second name space both are in a common native space all second that's why we can access this class a of second name space directly so now what I will do is I'll do this this will called unqualified classmate because we have not given any address so this is called unqualified but how will you now get the first class name so here we have first class name plus a so if we want to access the class name which is above this namespace okay so I will now say object is equal to new and here I have to give the full name space system so I will say slash then a okay so let's see the result it says second second area okay I have to comment this one reload and it says I am at second place I'm at global space so if I give here earned break reload and yeah first we are loading the image second say this one and then we are loading I'm at Google so this is a global space and this is called fully qualified class name or simply fq3 n okay so this is actually the name space system and keep in mind each in every file we have here is inside a real folder called name space but we have now made every class in new virtual directory so our index class and SNS class is inside this virtual second folder and this is inside a Google speed and I think now you are getting what is global space finally the name is this so there is another way of importing these classes so if I'd remove this seconds from here this means now this index dot PHP is at global space okay so if I want to access the class name a from second namespace so what I have to do I will use keyword use then second slash back slash a ok now whenever we are using a it will look for this one so let's go to clones reload and you can see first one is the second space and second one is the global space so first is this one which is actually getting this and this if I want to get then that is from the first name space which is at global space but suppose you don't want to do like this you want to do like this and because we are using class a new a every time so this is printing this but I want to get the last name a from the second name space so what I will do I will say have use this class as new a ok and if I say here new a let's see what happens when a new a reload and yeah first one is now from the second space what second one is now becoming the global space you can see this one is now the global because we have converted this file name a to new a for our index dot PHP okay so if you have any doubt feel free to message me comment me and don't forget to subscribe and like on Facebook Twitter and Instagram so we will meet in the Nexus so till then goodbye
Info
Channel: Bitfumes
Views: 36,927
Rating: undefined out of 5
Keywords: php oop, oop php, object oriented programming, object oriented programming php, php object oriented programming, oops in php, php oops, why we need oop, what is oop, namespace in php, namespace, namespace in oop, oop namespace, php oop namespace, php namespace, what is namespace, why use namespace, how to use namespace
Id: zTxz_kOLIZM
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Sun Aug 20 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.