As much as you've probably heard this question...
Moderator: General Moderators
As much as you've probably heard this question...
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Classes are useful when creating and passing objects to serve multiple purposes
an example would be
Although this is a very simple example, I hope it gives you some incentive on reading up on OOP. 
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
}
}
}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.
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.
Ah, yes, I thought this would be something useful.Phenom wrote:Classes are useful when creating and passing objects to serve multiple purposes
an example would be
Although this is a very simple example, I hope it gives you some incentive on reading up on OOP.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 it's a lot harder when trying to extract code from a class. >_<
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Why can't you simply use one of the functions?
can still be used outside of the class
Hope that is what you meant
Code: Select all
include 'class.php';
$user = new users();
if ($user->checklogin())
{
//do stuff
}Hope that is what you meant
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
Yeah, LPS got it. In your example, you'd have to extract code for checklogin() to get the function to work.Phenom wrote:Why can't you simply use one of the functions?
can still be used outside of the classCode: Select all
include 'class.php'; $user = new users(); if ($user->checklogin()) { //do stuff }
Hope that is what you meant