Page 1 of 1

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

Posted: Sun Feb 13, 2005 11:33 pm
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.

Posted: Sun Feb 13, 2005 11:50 pm
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.

Posted: Mon Feb 14, 2005 10:24 am
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. :)

Posted: Mon Feb 14, 2005 11:29 am
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.

Posted: Mon Feb 14, 2005 6:20 pm
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. >_<

Posted: Mon Feb 14, 2005 8:42 pm
by John Cartwright
Although it's a lot harder when trying to extract code from a class. >_<
Don't really understand what you mean

Posted: Mon Feb 14, 2005 9:02 pm
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.

Posted: Mon Feb 14, 2005 9:10 pm
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

Posted: Mon Feb 14, 2005 10:00 pm
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.

Posted: Tue Feb 15, 2005 10:20 am
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.

Posted: Tue Feb 15, 2005 10:27 am
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?

Posted: Tue Feb 15, 2005 11:03 pm
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.