Hi, I was wondering if I am going to make a social networking site if it was neccesary for me to understand how classes are used? Do i need to use classes or can I just use a series of functions?
garyjohnson 14 Junior Poster
pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster
Technically it would be no problem to this with functions only. In the long run however, you'll be better off understanding and (re)using classes/objects.
garyjohnson 14 Junior Poster
What is the benifit to classes?
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
What is the benifit to classes?
They help with the organization of large projects.
Edited by deceptikon
broj1 356 Humble servant Featured Poster
Classes are part of Object Oriented Programming concept (OOP). PHP first supported only traditional procedural programming style but with version 5 (and partly four) the OOP functionality was added. It has been mature for quite some time now and many functions have both versions (i.e. mysqli_query).
The benefit of OOP approach (or classes as you say) is easier maintenance, upgrading and better scalability of complex applications but provided that you design your classes appropriately. This takes practice to achieve. There is another benefit of being familiar with OOP in that many libraries and frameworks use OOP approach and it is good if you understand it well.
Edited by broj1
veedeoo 474 Junior Poster Featured Poster
You will be able to use or implement either one of the common design patterns. My favorite is the observer pattern and Singleton pattern.
Benefits in writing codes in OOP
1. One or more classes can be derived from a base class. This is called inheritance. As the application grows overtime, new methods are needed to accomodate the growth .
For example, if we have a bicycle class responsible for displaying information all about bicycle, years later our applications needs to provide information for both the bicycle and the motorcycle. For non-OOP programmers, the first thing that will come to mind to accomodate this change is to trash bin the original bycycle class and create a new one that will handle both. However, for the OOP programmers, the first thing they do is to analyze what makes motorcycle similar to bicycle. All features of motorcyle that are not in the bicycle will be aded in the extended bicycle class.
A simple example to clarify my statement 1 above.
class Bicycle{
public $wheels, $chain, $power;
public function __construct($wheels, $chain, $power){
$this->wheels = $wheels;
$this->chain = $chain;
$this->power = $chain;
}
public function get_wheels(){
return $this->wheels;
}
public function get_chain(){
return $this->$chain;
}
public function get_power(){
return $this->power;
}
}
Example of inheritance, extending the bicycle class above to create a new child class motorcycle.
class Motorcycle extends Bicycle{
public $wide_wheels, $wide_chain, $motor_power;
public function __construct($wheels, $chain, $power, $wide_wheels, $wide_chain, $motor_power ){
parent::__construct($wheels, $chain, $power);
$this->widerWheels = $wide_wheels;
$this->widerChain = $wide_chain;
$this->motorPower = $motor_power;
}
public function get_WideWheels(){
return $this->widerWheels;
}
public function get_WideChains(){
return $this->widerChain;
}
public function get_MotorPower(){
return $this->motorPower;
}
We can then set instances of both the parent and the child class like this..
$ObjectBike = new Bicycle('flimsy', 'single', 'foot power');
$ObjectHarley = new Motorcycle('ultra wid', 'gear drive', 'motor power');
A well written Class can take advantage of templating engine. Meaning all strings are passed or assigned to the template engine, without calling NOT a single echo inside the class methods.
By using a singleton pattern, we can make sure there is ONLY ONE instance of that class.
Application written in OOP can be converted later into MVC framework.
Advantage of functions over the confined methods within class.
1. Application can be deployed a lot faster, but this can lead to spaghetti like codes over time.
2. Requires less file. Normally, OOP written codes requires lots of files, because of separation of many different classes doing different jobs within the application.
3. Does not require highly organized bootstrapping. Normally, OOP based application requires a file manager script to handle the application's required files. Howerver, this is one of the unique properties of a well organized application.
<M/> 170 Why so serious? Featured Poster
This may help you learn more about classes. I found this on the php.net site.
http://php.net/manual/en/keyword.class.php
diafol
As pritaeas states, you can make it with procedural code / functions, but this would be unwise. Just because you can, doesn't mean that you should. AT first glance classes and their methods may seem like normal functions wrapped up in some class thingy. But this isn't the case. OOP / classes allow easier reuse and expansion. It allows you to compartmentalize your processes. Procedural code in big projects almost always runs into dependencies with all your data / code being coupled, meaning that a change in one bit, leads you to have to change another bit. Coding OOP-wise attempts to avoid this.
You'll find that your "client code" becomes a lot more manageable and succinct. All the hard, complicated stuff is handled by the classes, leaving you to write really lean code, which should be easy to debug. If you document your classes, it should be "easy" for another programmer to make use of your client code. If you're really serious about a big project, you may wish to collaborate, so providing an common, intuitive "interface" for doing stuff is vitally important. This is also important if you leave your project for a few days and come back to it and wonder what the hell you were thinking when you created some of your own pasta (spaghetti code).
Having said all that, OOP is a challenge, and if, like me you started out with procedural, you may find it mind-boggling to begin with. Too much terminology, too many patterns, too many frameworks! But, persevere, you just need to get your hands dirty and practise, practise, practise.
I mentioned frameworks earlier - I'd advise against jumping straight into these until you have a good grasp of OOP. It would be like planning a round-the-world solo drive in a new shiny automobile, without the faintest idea of how to fix it if it broke down. You only have to look at the forums in the framework sites to see that some people don't even know how to fill her up with gas.
pritaeas' tutorial is bang-on for beginners to OOP:
http://www.daniweb.com/web-development/php/tutorials/437592/introduction-to-phps-object-orientation
In addition, there's quite a nice article here comparing a simple procedural code with an OOP approach: http://www.coderprofile.com/coder/VBAssassin/blog/2/practical-use-of-the-factory-pattern-polymorphism-and-interfaces-in-php
Edited by diafol
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.