Jump to content

Classes


atholon

Recommended Posts

I would like to know why classes are so much better than functions?

 

The only thing I can think of is because they create a new individual function/variables without overwriting the original.

 

Lets say I have a function:

function addSomething($input)
{
$result = $input +1;
}

 

Why would a class be a better option?

Link to comment
Share on other sites

This question gets asked alot and to be honest, it isn't easily explained. Classes and OOP do allow for greater fexabilty and code re-use. You won't see the point though untill you get into using them and start implimenting design patterns.

Link to comment
Share on other sites

OOP really is a huge topic. If you are proficient in procedural code and would like to advance to using OOP style coding I would suggest getting a good book. Ive read PHP Objects Patterns and Practice several times and am still just getting my head around some of the ideas.

 

Another good thing to do is to download / install and use one of the popular frameworks developed using design patterns as best practice. Code ignitor is a nice simple one to start with. Once you've used it a little you'll understand some of the concepts, from there you can look through there code to get an idea of how it all works.

 

With that in mind, you may then like to impliment some of the ideas in your own code.

Link to comment
Share on other sites

If you want to learn OOP then I'd suggest Zend Framework to CodeIgnitor as you'll handle more things yourself thus dealing more directly with the classes yourself. Still, ZF probably has a higher learning curve than CI does, so for a beginner CI may be easier to comprehend. Something to read is the OOP section of the manual as well as some of the things in this topic.

Link to comment
Share on other sites

I would like to know why classes are so much better than functions?

a) their not... both have their unique uses and strengths/weaknesses (i prefer procedurial for php, so... i'll fight that case)

 

if you design procedural functions properly... they can work to a simlar scope as OOP... using the return; method, you can make it so you can have one function, giving different data to an unlimited amount of variables... which can then be used however you see fit... same as OOP only its FAR easier to jump between non-related functions...

 

however... if you were to use a client sided programming language(javascript for example) OOP wins hands down... you can edit code live, and access many different otherwize dormant areas, that procedurial would take FOREVER to get to... you can also use timers, which would be VERY inaccurate with procedurial!

 

i'm not saying procedurial is better then OOP or vice versa... just both have their differences... and both are equally as useable...

 

(in my opinion... OOP has no place in server sided languages... but! many people like it... so... its 100% down to the user :D)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.