Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,286 members, 7,957,742 topics. Date: Tuesday, 24 September 2024 at 06:49 PM |
Nairaland Forum / Science/Technology / Programming / Let's Learn Object Oriented PHP! (6201 Views)
Object-oriented PHP / A Beginners Guide To Object-oriented Programming (OOP) In PHP 5+ / Criticism Of Object Oriented Programming (2) (3) (4)
Let's Learn Object Oriented PHP! by timbs001(m): 11:10am On Mar 30, 2012 |
Most companies nowadays hiring PHP developers are requiring knowledge of Object oriented PHP as a requisite. I have worked with many Frameworks such as PEAR, Zend Framework and also Drupal CMS and all of them are developed using object oriented Approach. Most PHP developers are still comfortable with the old procedural way of coding and are seemingly scared of the word OOP. I want us to use this medium to enlighten everyone the advantages of OOP PHP over the old procedural way and also post relevant tutorials to help pple learn. Once pple signify interest , I will also start posting tutorials also. Pls let's keep the thread Educative and dedicated to PHP |
Re: Let's Learn Object Oriented PHP! by doncigalo: 11:17am On Mar 30, 2012 |
I'm game !! |
Re: Let's Learn Object Oriented PHP! by solokool(m): 12:08pm On Mar 30, 2012 |
i need it |
Re: Let's Learn Object Oriented PHP! by timbs001(m): 4:05pm On Mar 30, 2012 |
@donciaglo and solokool, noted |
Re: Let's Learn Object Oriented PHP! by AZeD1(m): 1:18pm On Mar 31, 2012 |
OK.....I'm game |
Re: Let's Learn Object Oriented PHP! by hello1212: 3:54pm On Apr 01, 2012 |
I'm game !! [img]http://www.dubaa.info/g.gif[/img] |
Re: Let's Learn Object Oriented PHP! by timbs001(m): 8:52am On Apr 02, 2012 |
@A-Zed and Hello1212, noted. We will kick off this week. Keep checking the thread for updates. Happy PHPing |
Re: Let's Learn Object Oriented PHP! by kodewrita(m): 2:02pm On Apr 02, 2012 |
timbs001: @A-Zed and Hello1212, noted. We will kick off this week. Keep checking the thread for updates. Happy PHPingGood stuff timbs001 and welcome. If this gets good enough, we might sticky it. All the best. |
Re: Let's Learn Object Oriented PHP! by Seun(m): 4:20pm On Apr 02, 2012 |
OOP PHP. Alright! I'm also thinking about creating threads for functional Visual Basic & high level machine language. |
Re: Let's Learn Object Oriented PHP! by mj(m): 7:22pm On Apr 02, 2012 |
I'm in. |
Re: Let's Learn Object Oriented PHP! by Nobody: 8:50pm On Apr 02, 2012 |
pulled from killerPHP.com Object Oriented PHP for Beginners: Steps 1 - 5 1 Like |
Re: Let's Learn Object Oriented PHP! by timbs001(m): 10:49am On Apr 04, 2012 |
TUTORIAL ONE: ADVANTAGES OF OBJECT ORIENTED PHP PROGRAMMING CODE REUSABILITY Breaking down complex tasks into generic modules makes it much easier to reuse code. Class files are normally separate from the main script, so they can be quickly deployed and reused in different projects. POLYMORPHISM Each class and object is independent, so method and property names are intrinsically associated with the class and any objects created from it. There’s no danger of naming conflicts, so methods(functions) or properties(variables) can have the same name as long as they exist in different classes. If you have two functions bearing the same name in a script written in the procedural way, it will cause a fatal error. In OOP PHP, you can have something like: <?php Class A { Public function isEmail(){ // code goes here } } Class B { Public function isEmail(){ // code goes here } } ?> There will be no error arising from the above code because each isEmail() function exist in a scope of its own, i.e encapsulated. SIMPLICITY AND READABILITY Imagine validating user form fields and all you see in the code is the following <?php // use class methods to validate individual fields $val->isInt('age'); $val->removeTags('name', 0, 0, 1); $val->checkTextLength('comments', 5, 500); $val->removeTags('comments', 0, 0, 1); $val->isEmail('email'); // validate the input and get any error messages $filtered = $val->validateInput(); $missing = $val->getMissing(); $errors = $val->getErrors(); ?> The above code is simpler and easier to read and understand as opposed to the procedural way. The details for validation and generating error messages is safely tucked away in the classes and Can be changed at anytime without affecting the implementations above in the client code. PROTECTING DATA INTEGRITY WITH ENCAPSULATION: The idea of encapsulation is to ensure that each part of an application is self-contained and doesn’t interfere with any others, except in a clearly defined manner. Each object will behave like a black box separate from even other objects of the same class and the data in them can be restricted by access level . IN TUTORIAL 2...... We will be talking about: what are classes and objects? and how to create classes and objects in PHP Later we will move on to advanced concepts such as[b] abstract classes and interfaces and also the different ways of generating objects such as Singleton pattern and the factory pattern.[/b] Feel free to ask questions Happy PHPing |
Re: Let's Learn Object Oriented PHP! by Nobody: 6:33pm On Apr 04, 2012 |
it's too early to jump into design patterns at least more should be shed on private, static,interface, abstract and mayne the new "traits" then reasons why to use PHP OOP before design patterns, design patterns for when the users is comfortable with OOP. |
Re: Let's Learn Object Oriented PHP! by timbs001(m): 6:40pm On Apr 04, 2012 |
@pc_guru, noted. But - didn't have intention of going into that soon anyway. Thanks for the tutorial post. Guess you know much about OOP PHP too. Looking forward to more postings from you. Happy PHPing |
Re: Let's Learn Object Oriented PHP! by Nobody: 6:49pm On Apr 04, 2012 |
yeah a lot i was reluctant at first but i did learn it,i mostly work with Frameworks and CMS so OOP for me na must, no p i had a tutorial on it somewhere let me search for it. |
Re: Let's Learn Object Oriented PHP! by Nobody: 6:55pm On Apr 04, 2012 |
Are you a PHP programmer, are you tired of trying to manage codes, Are you still writing procedural coding style? Constructors ? Instances of classes are created using the “new” keyword. What happens during the “new” call is that a new object is allocated with its own copies of the properties defined in the class you requested, and then the constructor of the object is called in case one was defined. The constructor is a method named __construct() , which is automatically called by the new keyword after creating the object. It is usually used to automatically perform various initializations e.g. class SampleAPP{ The Constructor is also the same name of the Class it is the code that is called when the Class is initialized Tip: Because a constructor cannot return a value, the most common practice for raising an error from within the constructor is by throwing an exception. Creating an Instance of a Class view plaincopy to clipboardprint? $sampleApp= new SampleAPP(); //Prints Hello World of OOP How Encapsulation Works Testing with Base Class view plaincopy to clipboardprint? class SampleAPP{ Testing with Child Class view plaincopy to clipboardprint? class SampleAPP{ Static Variable As you know by now, classes can declare properties. Each instance of the class (i.e., object) has its own copy of these properties. However, a class can also contain static properties. Unlike regular properties, these belong to the class itself and not to any instance of it. Therefore, they are often called As you know by now, classes can declare properties. Each instance of the class (i.e., object) has its own copy of these properties. However, a class can also contain static properties. Unlike regular properties, these belong to the class itself and not to any instance of it. Therefore, they are often called Gutmans_Frontmatter PHP 5 Creating a Static Property view plaincopy to clipboardprint?
Accessing Static Varaible view plaincopy to clipboardprint? MyClass::$myInitializedStaticVar++; Tip: self refers to the Class in the Current Context and parent refers to the parent class of the Sub-Class you are in $this cannot be used to access a static variable This is also Applicable view plaincopy to clipboardprint? print self::$myStaticVar; These can only be used within a context of a class, if not the classname must be given Static Methods Similar to static properties, PHP supports declaring methods as static. What this means is that your static methods are part of the class and are not bound to any specific object instance and its properties. Therefore, “$this” isn’t accessible in these methods, but the class itself is by using “self” to access it. Because static methods aren’t bound to any specific object, you can call them without creating an object instance by using the class_name::method() syntax. view plaincopy to clipboardprint?
Polymorphism in Action Suppose we were to create an application that prints out sounds that animals make, I might be tempted to create Dog Class then create a function called bark(),then a Cat function meow(), and a lion function Roar() view plaincopy to clipboardprint? class Dog{ What do you notice nothing wrong right, the code is actually right and it will run, but now let do something daring let’s make the sound of each animal if an object of an a particular one is created view plaincopy to clipboardprint? function MakeSound($obj) To be quite Frank there’s nothing actually wrong with this code except that imagine we had a zoo application and we have 200 different animals, it will be quite cumbersome if we compared the object to 200 animals, so we are going to review the code to a better alternative Step 1:Make a Parent Class view plaincopy to clipboardprint? class Animal{ Printing the Sound view plaincopy to clipboardprint? function MakeSound($obj) As you can see OOP eliminates the need of if’s and makes a Class Check to assert if $obj is an instance of Animal and if it is it will call the MakeSound method of that class Type Hinting Another useful feature, this forces Objects to be Parameters of a function, when specifying the Object Parameter it does a instanceof check on the object before performing the method view plaincopy to clipboardprint? public function EngineType(Automobile $auto) The Function EngineType will only accept a an object of the class Automobile and call the method getEngineType of the Automobile Class Arrays as Parameters view plaincopy to clipboardprint? class PseudoCode{ In the Pseudo Code above it takes an array as it argument and reads the values from the array passed, this is quite essential to adopt as frameworks like CakePHP,Zend and Pear make use of arrays of arguments Break Time Take a break and in our part 2, we will continue with Abstractions,Interface and Magic methodsit’s advisable to create a simple class and get familiar to the concepts also make references to PHP Manual for more explanation[/code] |
Re: Let's Learn Object Oriented PHP! by Nobody: 6:55pm On Apr 04, 2012 |
i wrote the article years ago here's the link http://studentscircle.net/live/2011/07/learning-oop-in-php/ |
Re: Let's Learn Object Oriented PHP! by DualCore1: 7:29pm On Apr 04, 2012 |
Weldone guys. OOP is the way forward. I personally do not like to change what isn't broken if I do not think the change will improve things. I over time, had written a lot of functions into a file and all I needed to do was include that file and my turnaround time is cut short in any project so I didn't see a real need for OOP but went on to learn it and use it. OOP even made me work faster. Everyone should try it. |
Re: Let's Learn Object Oriented PHP! by cr8tv: 7:38pm On Apr 04, 2012 |
@pc guru, God bless you for sharing this with us. I have been used to procedural php, pls which website do i download a concise course material that features oop php. I remain grateful. |
Re: Let's Learn Object Oriented PHP! by bumdish: 7:48pm On Apr 04, 2012 |
Cool thought but I would advise you not to waste your time. Just pick up CodeIgniter (a simple elegant PHP Framework, and U can still do you classic PHP with the framework). I constraints you to OOP PHP without you even knowing. You would also enjoy the power of MVC all join. You can be up and running in 2days. Once again, I'l advise don't waste you precious time. Cheers |
Re: Let's Learn Object Oriented PHP! by Nobody: 7:53pm On Apr 04, 2012 |
killerphp is good for OOP PHP for beginners, bumdish is right, rarely will you ever code an OOP Project from scratch OOP makes abstraction and extending classes neccessary you can learn other libraries, i use Zend and Yii as my main Frameworks but also CakePHP because of the team am working with (f***king team) but when you use these frameworks y :Dou get acquainted to OOP without evening knowing as for me i'd recommend Yii, its easy for me and Zend is for enterprise ish. but back to topic i will post on interface i just hope i can see an article or have time to write. nice work guys |
Re: Let's Learn Object Oriented PHP! by timbs001(m): 7:54pm On Apr 04, 2012 |
@bumdish, nah The advantages of OOP PHP are too immense to ignore it. You can't work with most PHP based CMS without understanding OOP PHP, and also Zend framework. PHP is fast becoming a full fledged Object oriented language As of PHP 5.4. To ignore it is to remain outdated And miss out the advantages and features. Never regretted learning OOP PHP. Happy PHPing @ Pc_guru, u are right, one can get acquinted with OOP PHP While working with PHP frameworks. I will recommend Zend framework or DRupal CMS those are the Ones I work with. |
Re: Let's Learn Object Oriented PHP! by ushafe(m): 7:55pm On Apr 04, 2012 |
So intersting |
Re: Let's Learn Object Oriented PHP! by Nobody: 8:04pm On Apr 04, 2012 |
for now Zend Framework 1 is having a total rewrite and believe me Zend 1 is too compilcated and slow, i think for now Yii ,CakePHP, codeigniter is good but Yii is the fastest and the easiest that i know, to grasp first Zend Framework 2 is almost out which is a bit too complex esp with new patterns, try Yii if you don't like Yii or find it hard i'll send my real address for you to beat me up . |
Re: Let's Learn Object Oriented PHP! by timbs001(m): 8:10pm On Apr 04, 2012 |
pc guru: for now Zend Framework 1 is having a total rewrite and believe me Zend 1 is too compilcated and slow, i think for now Yii ,CakePHP, codeigniter is good but Yii is the fastest and the easiest that i know, to grasp first Zend Framework 2 is almost out which is a bit too complex esp with new patterns, try Yii if you don't like Yii or find it hard i'll send my real address for you to beat me up . OK, I'll try to pick up Yii once I'm free from some projects And incase it's not as you promised, don't hesitate to send Your real address for me to..... |
Re: Let's Learn Object Oriented PHP! by Nobody: 8:12pm On Apr 04, 2012 |
ok |
Re: Let's Learn Object Oriented PHP! by TrustZee(m): 8:35pm On Apr 04, 2012 |
Wana learn php |
Re: Let's Learn Object Oriented PHP! by emonkey(m): 8:37pm On Apr 04, 2012 |
timbs001: Most companies nowadays hiring PHP developers are requiring knowledge of Object oriented PHP as a requisite. I have worked with many Frameworks such as PEAR, Zend Framework and also Drupal CMS and all of them are developed using object oriented Approach. Most PHP developers are still comfortable with the old procedural way of coding and are seemingly scared of the word OOP. I want us to use this medium to enlighten everyone the advantages of OOP PHP over the old procedural way and also post relevant tutorials to help pple learn. Once pple signify interest , I will also start posting tutorials also. Pls let's keep the thread Educative and dedicated to PHP Point of correction . Drupal is not in OOP |
Re: Let's Learn Object Oriented PHP! by timbs001(m): 8:41pm On Apr 04, 2012 |
e-monkey:@e_monkey, Drupal 7 and 8 (the version I work with) is written in Object Oriented PHP.(Might not be 100% OOP anyway) Drupal is a PHP based content management system(CMS) Thanks for dropping by the thread anyway |
Re: Let's Learn Object Oriented PHP! by Nobody: 8:51pm On Apr 04, 2012 |
Corrections please: Drupal is a Content Management Framework and it is also a Content Management System at the same time. . . .caramba So all the OOP jargons means that i can ehm, create a class called say goat, and now create instances of the class e.g. $doncigalo = new GOAT('meee'); //just kidding. So is the class the object now or the goat? I seem not to remember. . . . .caramba |
Re: Let's Learn Object Oriented PHP! by member479760: 9:01pm On Apr 04, 2012 |
OP google it, u will find a lot of freebies. if wanna pay go lynda.com start from there. |
Re: Let's Learn Object Oriented PHP! by Nobody: 9:08pm On Apr 04, 2012 |
Aye, that linda babe is very good seriously. I have learnt a lot of great stuffs for there. |
Ekiti Government To Train Youths In Programming / Sorting Out A Simple Text Dictionary With PHP / How Can I Practice Programming on My PC?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 84 |