I currently have a fairly large application written entirely with procedural PHP. I am looking to further my PHP experience and recode a majority of my application using object-oriented techniques.
There are a lot of areas where OOP can help reduce the amount of code and make it easier to read. However, I have a few questions.
1) It is my understanding that one class is used as a blueprint for any number of objects, but any one class represents only one object, never more than one. So one class could represent a player, but never multiple players.
2) Since I will have quite a few different classes to include, do I use a "Loader" class to load them all using spl_autoload_register
or do I just use spl_autoload_register
in the program files for my application?
Edit: So my autoloader would be a class that I then create an instance of to start the autoloading or simply a php file with the function and the spl_autoload_register that I would include to avoid repeating the same code in multiple files?
3) Some of my classes depend on other classes. I've never encountered this before, so I honestly do not know the answer. If I include all the classes in my main program file, but my player class does not include the class which it needs to function, will the player class work since the main program has included the class which player depends on?
Edit: So even though one class may instantiate an object of type Player, and the Player class is not directly included by this class, it will still work because the controller class does include the Player class?
4) There are multiple cases where I will need to work on the objects I am creating. I am wondering how I should do that. For example, in my Player class I will sometimes need to send something from one Player to the other Player. So, do I implement a static method in the Player class that takes two Players as parameters and does the transfer or do I do something else?
Edit: Okay, so avoid static methods. Now I have a serious problem: I have methods that are ran multiple times in my application, but I cannot implement them as static methods. Am I supposed to implement them as instance methods? For example, sending from one Player to another. Would I create an instance method that takes a Player object and sends either to it or from it?
5) I have a lot of methods that don't really belong to any one instance of a class nor are they really appropriate as static methods. Should these be declared in their own class as static methods as Common or similar? What is the done in practice in this situation?
Edit: Would these methods belong in the specific application file for which they are used or perhaps stored in their own "functions.php" file?
6) I'd like to learn how to use namespaces, but my code will never be used by others and I will never use anyone else's code in my application. Are namespaces an unncessary addition in my application or would it be a good idea to learn how to use them? Regardless, does one application have one namespace (the application name?) or does each class belong to it's own namespace?
7) Lastly, is it common to have one class for database connections and also a class for network methods? My application needs both. I think the main problem I am having with converting my code to use object-oriented techniques is determining which methods to put where, as currently they are all in one monolithic file.
Thanks for any help and insight you can provide.
Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions. Object-oriented programming has several advantages over procedural programming: OOP is faster and easier to execute.
PHP is a server-side scripting language, mainly used for web development but also used as a general-purpose programming language. Object-Oriented Programming (PHP OOP), is a type of programming language principle added to php5, that helps in building complex, reusable web applications.
The PHP language itself is not object oriented. The Ruby language is object oriented, so is Java with exception of the primitive types. PHP is a hybrid language capable of objects creation, so is Delphi.
What is OOPs? Object Oriented is an approach to software development that models application around real world objects such as employees, cars, bank accounts, etc. A class defines the properties and methods of a real world object. An object is an occurrence of a class.
1) It is my understanding that one class is used as a blueprint for any number of objects, but any one class represents only one object, never more than one. So one class could represent a player, but never multiple players.
A class doesn't represent anything because as you correctly stated it is just a blueprint for any number of objects. You can have multiple instances (objects) of the same class representing multiple players.
2) Since I will have quite a few different classes to include, do I use a "Loader" class to load them all using spl_autoload_register or do I just use spl_autoload_register in the program files for my application?
Without knowing what you mean by "program files for my application" I say yes. Use an autoloader, because it just works and you don't have to worry about doing require_*
.
3) Some of my classes depend on other classes. I've never encountered this before, so I honestly do not know the answer. If I include all the classes in my main program file, but my player class does not include the class which it needs to function, will the player class work since the main program has included the class which player depends on?
You don't want to load all the classes, but only the ones you are going to use. Which again is not something you have to worry about when using an autoloader. But yes once a class is loaded it can be instantiate throughout the application.
Normally you would want to start the autploader in the bootstrap phase of the application.
4) There are multiple cases where I will need to work on the objects I am creating. I am wondering how I should do that. For example, in my Player class I will sometimes need to send something from one Player to the other Player. So, do I implement a static method in the Player class that takes two Players as parameters and does the transfer or do I do something else?
Avoid using static methods in OOP PHP. It is almost never needed. You could think about having another class which act like a pool of players which takes care of "sending data" from one player to another.
So it doesn't matter where the file with the class is included as long as it happens before trying to instantiate it.
5) I have a lot of methods that don't really belong to any one instance of a class nor are they really appropriate as static methods. Should these be declared in their own class as static methods as Common or similar? What is the done in practice in this situation?
Again please don't use static methods. If you use them inside some other class you are only making it hard to unit test your classes and you introduce hidden dependencies. In my experience it almost never happens to just have a single method somewhere for something. Perhaps you're methods are doing too much.
But that is hard to tell by just reading your question. Maybe you can give an example in a comment?
6) I'd like to learn how to use namespaces, but my code will never be used by others and I will never use anyone else's code in my application. Are namespaces an unncessary addition in my application or would it be a good idea to learn how to use them?
Namespacing in PHP is about structuring. Although nobody else will use your code you don't have to worry yourself about using the same name for a class somewhere. Which will happen once the application grows larger rather sooner than later.
Regardless, does one application have one namespace (the application name?) or does each class belong to it's own namespace?
I often follow PSR-0 when it comes to namespaces.
7) Lastly, is it common to have one class for database connections and also a class for network methods? My application needs both. I think the main problem I am having with converting my code to use object-oriented techniques is determining which methods to put where, as currently they are all in one monolithic file.
Just keep the Single Reponsibility Principle in mind and in general SOLID.
Also I highly suggest to watch these and keep watching them until you understand it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With