EvilZone

Programming and Scripting => Web Oriented Coding => : m0l0ko February 04, 2013, 01:19:31 AM

: PHP - What are classes all about?
: m0l0ko February 04, 2013, 01:19:31 AM
I'm trying to learn the concept of classes so I'm looking for a reason to use them but I find the whole concept might confusing. I can't yet see why I would use a class instead of just using a function. I don't understand why in classes you assign variables that are either public, protected or private, how does assigning a private or protected variable have any advantage over a regular public variable?

Thats besides the point though, I'm trying to figure out situations in which I would need to use classes so I can start practicing with them and implementing them into my code.
: Re: PHP - What are classes all about?
: Mike245 February 04, 2013, 01:53:07 AM
Using classes and objects is all about proper Object Oriented Programming. They are used to structure your code and provide proper encapsulation. It also makes it easy to implement polymorphism. I have never done PHP though. The biggest reasons for using OOP is re-usable code and polymorphism.


I hope this helps.
: Re: PHP - What are classes all about?
: m0l0ko February 04, 2013, 03:10:32 AM
I still don't understand when and where to use classes. My PHP scripts usually consist of a big page of functions, then a bit of code that calls the functions. What advantage would uses classes have over this? I notice you can put functions inside capsules which I like but I don't really know why I would wanna put functions inside a class.
: Re: PHP - What are classes all about?
: Mike245 February 04, 2013, 03:24:52 AM
Anytime you want to have data grouped together instead of having a mass of fuctions and global variables, use classes. It can also help with keeping code organized.


Classes help with organizing data and functions together.


I come from a background of strictly OOP. C#, Java, Objective-C. So it just comes naturally to me. I'm not sure how to better explain it though.
: Re: PHP - What are classes all about?
: proxx February 04, 2013, 04:57:53 AM

http://web.engr.oregonstate.edu/~budd/Books/oopintro3e/info/chap01.pdf (http://web.engr.oregonstate.edu/~budd/Books/oopintro3e/info/chap01.pdf)

This is a snippet of some book about OOP in general.
Its funny how its uses ; the flower boy, the driver etc etc as examples.
Helped me to grasp the concept.


https://www.youtube.com/watch?v=tWIe9E4SWQo
This is fucking awesome.