Hey everyone, I'm new on here (i have introduced myself here: viewtopic.php?f=6&t=9704&start=870 )
My question is about why we use classes and OOP rather than functions. I am not yet doing this although i have tried, but i keep finding myself using classes in exactly the same way as I would use a function.... only it takes more code. Now, I know that's not how they are supposed to be used but my head just seems to work that way.
For example, I will have some functions that I like to use often in a separate php file, then when I want to use them, I include the file at the top of my script and run the functions when I need them.
I understand that you can use classes the same way but I'm sure there is more to them than that. Sometimes i even call functions by a variable $myFunctionName($myParams) but I dont know any more efficient way of coding. I'm sure classes are more efficient, but I cant get my head around WHEN they are more efficient.
Can we please discuss how and when I should use classes and how they can be more effective?
OOP and Classes. can you help me understand the theory?
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: OOP and Classes. can you help me understand the theory?
This is a huge subject that you will not understand until you see object solving your own problems. But probably the most important thing is that you are still thinking about the code and not the data. You can consider a class a namespaced function library. Even that is an improvement over functions. But when you start thinking about your problems from the point-of-view of the data, then you will start to see the benefits of using objects. The idea is to first think about the domain of your problem and then about the code that operates on that data. The you can start passing data around and the data know what code can operate on it. It opens up a whole world of patterns and design ideas like Tell, Don't Ask, etc.mechanism wrote:Can we please discuss how and when I should use classes and how they can be more effective?
(#10850)
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: OOP and Classes. can you help me understand the theory?
A short non-technical answer would be that classes makes it easier to re-use your code
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Re: OOP and Classes. can you help me understand the theory?
thanks for the advice, can anyone give an example where object has solved a problem for them?
-
thinsoldier
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
Re: OOP and Classes. can you help me understand the theory?
I have some very poor examples.mechanism wrote:thanks for the advice, can anyone give an example where object has solved a problem for them?
I had a function that could accept about 20 different parameters but most of the time I only used 2 to 5 arguments.
So my code looked like
select_builder ( null, null, 'report', null, false, null, " ", true, 'directory', null, null, null, null, 'key', 'Any Report', null ) ;
By turning it into an object with the most common parameters as the constructor and the 2nd most common paramaters as another method and all other paramaters as class variables my code now looks like:
$select = new selectBuilder( 'report', $options, 'key', 'Any Report' );
// add a few infrequently used options;
$select->heirarchyField = 'directory';
$select->optGroups = true;
$select->multiple = true;
// invoke the tostring method and print a <select>
echo $select;
Is it more than the 1 line of code it was before, yes. But it's much more flexible and much easier to understand since you actually see the names of the un-common parameters and it doesn't matter which order I define the values in and I can omit parameters without worry.
The simplest use of classes is when you have a lot of functions related to a specific task but they have very generic names. Then you take these new functions and want to use them on a project you built 4 years ago that already had a few functions with the same names as your functions. You get function name conflict errors and have to rename a bunch of functions and rename everywhere those functions were used.
If your functions are in an object you can keep all the same function names and just rename the class name.
I have a series of sites that I've managed for 5 years and every time I go back to update the oldest site to make it better than the youngest site all I need to change is
$listingmodel = new ListingModel2004();
$helper = new HelperMethods2006();
to
$listingmodel = new ListingModel2011();
$listingmodel->behaveLike( new LegacyListingModelDecorator( 2004 ) );
$helper = new HelperMethods2010();
The names of the functions in all the classes are exactly the same. What the functions produce is exactly the same. But how I wrote sql queries in 2004 was was really dumb and slow but they queries worked. Sometimes they were so slow they took over 16 seconds to find anything but they eventually found it. Since then I've learned about proper table normalization and indexing and join queries and so I run functions with the same name as they had before but the way they use sql internally is much MUCH faster.
The code in the site that makes use of the info returned by the function doesn't care how the function got its info.
---
class guest extends simpleUser{...}
class author extends guest{...} // author user objects can do everything guest user objects as well as post/edit articles
class manager extends author{...} // managers can do everything authors can do, they can also approve/deny articles posted by authors and make new author accounts
class administrator extends manager {...} // admins can do everything managers can do as well as make/edit new accounts of all types and access the more technical areas of the menu system.
class webdeveloper extends administrator {...} // godmode
Warning: I have no idea what I'm talking about.
Re: OOP and Classes. can you help me understand the theory?
@thinsoldier.
thats some very good examples you have made. thanx. @ mechanism... just read about theory parts of Functional programming/ OOPS. you will get the diiference and think about data not code.
thats some very good examples you have made. thanx. @ mechanism... just read about theory parts of Functional programming/ OOPS. you will get the diiference and think about data not code.
Re: OOP and Classes. can you help me understand the theory?
oops and classes are a problem for me .
I am not any idea how i use the oops in a program.
Thanks for discuss this in this forum.
I am not any idea how i use the oops in a program.
Thanks for discuss this in this forum.
Re: OOP and Classes. can you help me understand the theory?
There are really 3 main ideas behind using OOP inside an application.
1. Code Re-Use - You can very easily just pick up a class object you have written previously, for example, a class to hand sessions or users.
2. Code Reduction - This is possibly the greatest benefit to OOP, by writing class methods to do extremely specific tasks, you can implement those tasks inside other ojects, which means you dont need to write replica code.
3. Code Debugging - If you have ever had the problem of looking through a 3000 line PHP script, then you will know that if you have no idea what your error is coming from, then you will understand the benefit of debugging. Each method of a class SHOULD have a RETURN value, which can be tested using Unit Testing.
@Riyaverma:
The best way to start would be to implement a class object which can do something extremely simple, possibly something like form validation or error handling.
1. Code Re-Use - You can very easily just pick up a class object you have written previously, for example, a class to hand sessions or users.
2. Code Reduction - This is possibly the greatest benefit to OOP, by writing class methods to do extremely specific tasks, you can implement those tasks inside other ojects, which means you dont need to write replica code.
3. Code Debugging - If you have ever had the problem of looking through a 3000 line PHP script, then you will know that if you have no idea what your error is coming from, then you will understand the benefit of debugging. Each method of a class SHOULD have a RETURN value, which can be tested using Unit Testing.
@Riyaverma:
The best way to start would be to implement a class object which can do something extremely simple, possibly something like form validation or error handling.
Code: Select all
<?php
class Error extends Exception{
public function __toString(){
return "[".__CLASS__."] Error: ".$e." has occurred.";
}
}
class Checking{
public function checkName(string &$name){
try{
if(!empty($name) || $name != "Weiry"){
throw new Error("Not Name");
}
return true;
catch( Error $e){
print $e;
return false;
}
}
}
$check = new Checking();
$name = ($check->checkName("Weiry") == true) ? "Weiry" : "unknown";
print "Name: ".$name;
$name2 = ($check->checkName("NotWeiry") == true) ? "Weiry" : "unknown";
print "Name: ".$name2;