February 17, 201016 yr Right, Really struggling moving over to OOP style of programming, Any tips or pointers? Much appreciated. Ben
February 17, 201016 yr Right, Really struggling moving over to OOP style of programming, Any tips or pointers? Much appreciated. Ben Hey Ben, what is it you are struggling with? Understanding classes & objects or applying it? btw, did you get your .htaccess working? I replied again, to that thread. Dave
February 17, 201016 yr OOP is a way of encapsulating related properties and methods together, ultimately to make your life as a programmer easier and faster to create an application A classic example of a class is a "person" class - Below is a simple one class person { /* THESE ARE THE CLASS PROPERTIES (ie it's attributes) */ // The persons age private $_age; // The persons name public $name; /* THESE ARE THE CLASS METHODS (functions are called methods in classes) */ // Public method accessible outside of the class to show the persons age public function showage() { echo $this->_age; } // Public method to output hello by the current person public function say_hello() { // Here we call the _getname() method, accessible only within the // class itsself echo 'Hello I\'m '.$this->_getname(); } // This is not accessible outside of the object code private function _getname() { return $this->name; } // The __construct method is always public and is called when the // object is initially created function __construct($age = 20) { // Assign the passed age to the person property $this->age = $age; } } To create a new person we call it with the new keyword and pass parameters if required (this calls the __construct method in the code above). In this example the $age is optional as it has a default value of 20, so it will assign 20 to the objects age unless we give a value. I've used 25 in the example below. Next I directly assign a name to the $name property of Jay Gilford. Note that you cannot do this with private variables (like the $_age) - private variables are only manipulated by the class code itself using the $this->_age $jay = new person(25); $jay->name = 'Jay Gilford'; Note that the syntax to access the properties is $object->property. Methods are similar, only they have parentheses after them, and work just like your average function does $jay->say_hello(); This runs the say_hello method that would output Hello I'm Jay Gilford. Since the _getname() method is private, you cannot do echo $jay->_getname(); as again it's only accessible within the class itself The real benefit of objects is you create a "blueprint" of the object, and can make multiple objects from that object with different properties. Say for example I then go ahead with $irn3rd = new person(); // Creates a new person with an age of 20 as no age specified $irn3rd->name = 'irn3rd'; // Assigns the name // Show both ages echo 'Jay is'.$jay->showage().'<br />'; echo 'irn3rd is '.$irn3rd->showage(); Which as you can see although they run through the same class, they both show independent values in their properties (25 and 20) There is far more to objects but this should get you started with the basics Other than that, I would suggest you look into tutorialized for OOP PHP tutorials for more advanced articles As a final note, you will notice that I put an underscore before the names of properties and methods that are private. This is NOT a requirement nor does it automatically assign them as being private, it's merely a way of separating the two visually which many programmers adopt in their coding, and I would advise you do the same with your code Hope that gives you some better understanding and I've not just wasted 25 minutes of my life writing this Jay Gilford
February 17, 201016 yr Author Hey Ben, what is it you are struggling with? Understanding classes & objects or applying it? btw, did you get your .htaccess working? I replied again, to that thread. Dave I'm struggling in applying it, I understand basics of how it works, as i have only just started, more i do it more i'll learn obviously. But with a project i have, i have a process file( all different functions and process to do) Its converting that into OOP, i know all the functions and processes in the file will be separated into their own classes, its just implementing it. so i'm struggling with applying it an all sense. And yeah i got my .htaccess working a charm now, posted in the thread.. Owe you one Ben OOP is a way of encapsulating related properties and methods together, ultimately to make your life as a programmer easier and faster to create an application A classic example of a class is a "person" class - Below is a simple one class person { /* THESE ARE THE CLASS PROPERTIES (ie it's attributes) */ // The persons age private $_age; // The persons name public $name; /* THESE ARE THE CLASS METHODS (functions are called methods in classes) */ // Public method accessible outside of the class to show the persons age public function showage() { echo $this->_age; } // Public method to output hello by the current person public function say_hello() { // Here we call the _getname() method, accessible only within the // class itsself echo 'Hello I\'m '.$this->_getname(); } // This is not accessible outside of the object code private function _getname() { return $this->name; } // The __construct method is always public and is called when the // object is initially created function __construct($age = 20) { // Assign the passed age to the person property $this->age = $age; } } To create a new person we call it with the new keyword and pass parameters if required (this calls the __construct method in the code above). In this example the $age is optional as it has a default value of 20, so it will assign 20 to the objects age unless we give a value. I've used 25 in the example below. Next I directly assign a name to the $name property of Jay Gilford. Note that you cannot do this with private variables (like the $_age) - private variables are only manipulated by the class code itself using the $this->_age $jay = new person(25); $jay->name = 'Jay Gilford'; Note that the syntax to access the properties is $object->property. Methods are similar, only they have parentheses after them, and work just like your average function does $jay->say_hello(); This runs the say_hello method that would output Hello I'm Jay Gilford. Since the _getname() method is private, you cannot do echo $jay->_getname(); as again it's only accessible within the class itself The real benefit of objects is you create a "blueprint" of the object, and can make multiple objects from that object with different properties. Say for example I then go ahead with $irn3rd = new person(); // Creates a new person with an age of 20 as no age specified $irn3rd->name = 'irn3rd'; // Assigns the name // Show both ages echo 'Jay is'.$jay->showage().'<br />'; echo 'irn3rd is '.$irn3rd->showage(); Which as you can see although they run through the same class, they both show independent values in their properties (25 and 20) There is far more to objects but this should get you started with the basics Other than that, I would suggest you look into tutorialized for OOP PHP tutorials for more advanced articles As a final note, you will notice that I put an underscore before the names of properties and methods that are private. This is NOT a requirement nor does it automatically assign them as being private, it's merely a way of separating the two visually which many programmers adopt in their coding, and I would advise you do the same with your code Hope that gives you some better understanding and I've not just wasted 25 minutes of my life writing this Jay Gilford Thanks for your detailed explanation Jay, It's cleared a few things up, will have to read through it a few times to understand it completely, as i haven't slept in over 18 hours so bit tired, more i read more i'll learn, I'm subbed to someone on youtube who is doing tutorials atm so i'm learning a bit from him. I'll look into tutorialized more, And don't worry mate its not wasted time, its knowledge passed on from one to another, and wont be forgotten Just wish there was something i could do for people that help me in my learning. Damn college teaching procedural to me, damn them. Many thanks so far all much ablidged! Ben
February 17, 201016 yr Implementing objects in itself is very simple - they are simply a wrapper around a collection of methods(functions) and properties(variables). What is less obvious is their application in what is known as a design pattern. Design patterns are common methods of using objects to solve a problem. MVC (Model View Control) is a famous design pattern, but there are many others, like Decorator, Composite, Strategy etc. Take a simple problem, like form validation (and creation). The form itself could be considered an object. Then think of each element in the form - these too could be considered as objects. So, I could make a Form class, which contains an array called "elements" - and a method called addElements. i.e: class Form{ private $elements = array();//no-one else needs to access this, so I'll just make it private public function addElements(Element $element){ //by adding the name of the expected class in the arguments brackets - I enforce a type constraint $elements[]=$element; } } This now allows me to add elements to my form. So, better make an element class. But wait, while there is only one type of form, there are lots of elements. Time for some inheritance. First, I'll make an abstract that all the elements can inherit common properties/methods from: abstract class Element{ protected $type protected $validators = array(); public function addValidator(Validator $validator){ //can you see where this is going? } public function validate(){ foreach($validators as $v){ //run validation on the input, return messages } } } I can't use that class directly - so I have to EXTEND it. class TextElement extends Element{ } This time though, I don't have to write all those functions, because this class will inherit them from it's parent. So now we can create elements, and add them to the form. How would I run validation though? Better add a method to the Form class public function validate(){ $result = array();//to capture the output foreach($elements as $e){ $result[] = $e->validate(); } } So now, when you call validate on the Form, it automatically calls validate on all it's elements. This is a really basic example. You might argue that using so many objects for a single form might be a bit heavy, but I found it helps to see OOP used for something practical. The Form class demonstrates a design pattern called Composite - in that it is a collection of similar objects - which in turn illustrates an OOP principle, favour composition over inheritance. These are whole topics on their own. In addition, there are some nifty mechanics you can exploit in OOP, like static functions/properties, magic methods like __call (very handy, automates method delegation). The real power in OOP is not in one object, but in how you COMBINE several objects - don't just look at them as a wrapper for a load of procedural code. Take any application, or product, and it will have "entities" - i.e. things that might map really closely to the real world (so, for Amazon, that might be customers, products, orders). It will also have lots of other aspects that don't. Look at your application, and try to separate each element based on it's responsibility. So - taking our form example, should a form handle validation for all the elements it contains, or should it leave that to each element to take care of itself? At the end of the day, these are all optional design decisions you have to make. You could make one class that does everything. It would break every principle of OOP, run like a dog and basically be a big wrapper for a procedural program - but that is the interesting aspect of OOP - it makes application programming more about design, and less about seeing if you closed a bracket in your 10th conditional statement... Here are some excellent books: http://www.amazon.co.uk/PHP-Objects-Patterns-Practice-2nd/dp/1590599098 This one is brilliant, my copy is dog eared to hell. However, I warn you, it's very abstract and might put you off even more - but one you'll come back to again and again. I'd start with the main chapters, and then leave it alone for a while when it hits the design patterns - for which I'd check out the next book... http://www.wrox.com/WileyCDA/WroxTitle/Professional-PHP-Design-Patterns.productCd-0470496703.html This book is way more usable, and shows some common sense use of design patterns. If you want to demystify OOP, this is the one. I also found these tutorials on PHPFreaks to be the most straightforward: http://www.phpfreaks.com/tutorial/oo-php-part-1-oop-in-full-effect http://www.phpfreaks.com/tutorial/oo-php-part-2-boring-oo-principles This one in particular helped me start understanding patterns, and how important they are http://www.phpfreaks.com/tutorial/design-patterns---strategy-and-bridge Hope that helps!
Create an account or sign in to comment