![]() ![]() ![]() With namespaces, we can avoid the headache of naming collisions when mixing third-party code with our own projects. If we didn't have namespaces we'd have to (potentially) change a lot of code any time we added a library, or come up with tedious prefixes to make our function names unique. Or we can declare that we're in one of the namespaces and then we can just call that namespace's output(): namespace MyProject Later when we want to use the different functions, we'd use: \MyProject\output() That would look something like this: namespace MyProject We stick each output() function in its own namespace. How do we solve having two output() functions? Simple. When you call output(), how does PHP know whether to use your output() function or the RSS library's output() function? It doesn't. This library also uses a function named output() to output the final feed. You add a library that allows you to generate RSS feeds. Later on your application gets bigger and you want to add new features. Your output() function takes all of the HTML code on your page and sends it to the user. Suppose you write an application that uses a function named output(). If there are two people named "John" you can use their surnames to tell them apart. In simple terms, think of a namespace as a person's surname. It allows you to use the same function or class name in different parts of the same program without causing a name collision. Namespacing does for functions and classes what scope does for variables. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |