As much as you've probably heard this question...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
windwaker
Forum Newbie
Posts: 21
Joined: Sun Feb 13, 2005 11:18 am

As much as you've probably heard this question...

Post by windwaker »

You'll have to bear with me. ;_;

I've done a lot of PHP, and some C, and I was wondering; why use classes, other than for organization? I mean, I use them for the sake of good code practice, but I really don't know why else you owuld want to, unless it speeds up parsing somehow.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

unless you are using OOP, classes are pretty much just an organizational thing. At that level, you're missing about 80% of the power of a language.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Classes are useful when creating and passing objects to serve multiple purposes


an example would be

Code: Select all

class users 
{
     function checklogin()
     {
          return isset($_SESSIONї'logged']);
     }
 
     function editprofile()
     {
           if ($this->checklogin())
           {
               //do stuff
           }
     }

     function someotherfunction()
     {
           if ($this->checklogin())
           {
               //do stuff
           }  
     }    

}
Although this is a very simple example, I hope it gives you some incentive on reading up on OOP. :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

basic principles of OOP:
encapsulation
inheritance
polymorphism

I know these principles can be achieved without classes too, but it is much easier to have a framework that helps you achieve those things. In my eyes C++ is such a framework.

For example i could have a base class and a class that inherits from that class and overrides/extends a function. Now, i could easily write my own switch and pass a pointer to the desired function, but i prefer the language/compiler to do that for me.
windwaker
Forum Newbie
Posts: 21
Joined: Sun Feb 13, 2005 11:18 am

Post by windwaker »

Phenom wrote:Classes are useful when creating and passing objects to serve multiple purposes


an example would be

Code: Select all

class users 
{
     function checklogin()
     {
          return isset($_SESSIONї'logged']);
     }
 
     function editprofile()
     {
           if ($this->checklogin())
           {
               //do stuff
           }
     }

     function someotherfunction()
     {
           if ($this->checklogin())
           {
               //do stuff
           }  
     }    

}
Although this is a very simple example, I hope it gives you some incentive on reading up on OOP. :)
Ah, yes, I thought this would be something useful.

Although it's a lot harder when trying to extract code from a class. >_<
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Although it's a lot harder when trying to extract code from a class. >_<
Don't really understand what you mean
windwaker
Forum Newbie
Posts: 21
Joined: Sun Feb 13, 2005 11:18 am

Post by windwaker »

Well, since classes sometimes share functions like that, you can't just extract one function, you have to either use the class, or extract all the necessary functions, which takes time.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why can't you simply use one of the functions?

Code: Select all

include 'class.php';
$user = new users();

if ($user->checklogin())
&#123;
     //do stuff
&#125;
can still be used outside of the class

Hope that is what you meant
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

He means if say, you have a class of general functions, but you'd only like to use two, you need to extract them and get rid of any class dependancies they have.

Although, I find this 100% easier than going through mixed procedural code.
windwaker
Forum Newbie
Posts: 21
Joined: Sun Feb 13, 2005 11:18 am

Post by windwaker »

Phenom wrote:Why can't you simply use one of the functions?

Code: Select all

include 'class.php';
$user = new users();

if ($user->checklogin())
&#123;
     //do stuff
&#125;
can still be used outside of the class

Hope that is what you meant
Yeah, LPS got it. In your example, you'd have to extract code for checklogin() to get the function to work.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

the concept of oop is that you group functions and data that belong to each other... just as you would include some library in c...

don't see why this should be a problem?
windwaker
Forum Newbie
Posts: 21
Joined: Sun Feb 13, 2005 11:18 am

Post by windwaker »

Yes, from the C I've done, I know what it does, I was just wondering if that was the only purpose. Which it appears that is the only purpose.
Post Reply