PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Feb 23, 2020 12:39 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Oct 27, 2011 5:55 pm 
Offline
Forum Newbie

Joined: Thu Oct 27, 2011 4:43 pm
Posts: 5
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?


Top
 Profile  
 
PostPosted: Sun Oct 30, 2011 10:28 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Mon Oct 31, 2011 4:21 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
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


Top
 Profile  
 
PostPosted: Sat Nov 05, 2011 7:46 pm 
Offline
Forum Newbie

Joined: Thu Oct 27, 2011 4:43 pm
Posts: 5
thanks for the advice, can anyone give an example where object has solved a problem for them?


Top
 Profile  
 
PostPosted: Sat Nov 26, 2011 4:00 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Tue Jan 17, 2012 10:02 pm 
Offline
Forum Newbie

Joined: Fri Dec 09, 2011 1:12 am
Posts: 14
@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.


Top
 Profile  
 
PostPosted: Fri Feb 03, 2012 6:08 am 
Offline
Forum Newbie

Joined: Fri Feb 03, 2012 2:14 am
Posts: 2
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.


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 6:38 am 
Offline
Forum Contributor
User avatar

Joined: Wed Sep 09, 2009 5:55 am
Posts: 323
Location: Australia
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.
Syntax: [ Download ] [ Hide ]
<?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;
 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group