Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
I still dont really gte the point of Classes...
Old 09-14-2007, 12:20 PM I still dont really gte the point of Classes...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Okay,

i was asking about classes a while ago.. so i took out (and in a few cases re-borrowed) some of the more advance books for library, and just been looking at a simpler one and i think i kind of get the structure.

but it just seens like its nothing special and i jsut kind of think it could all be done alot simpler with just plain old functions...

Could someone please try and make me get this!
and if possible post/linkto a (well commeted) script which might help me understand why they should be used

Thansk alot,
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 09-14-2007, 01:44 PM Re: I still dont really gte the point of Classes...
Ultra Talker

Posts: 483
Trades: 0
Classes are a part of what is called 'object oriented' programming.

The idea of classes is that each class represents 'something' and that that 'something' is able to protect you, the coder, from how it's internally represented.

So how to explain that? I might have a class called 'person'. Now imagine that you are using this class. The class has a pair of functions called 'GetName' and 'SetName'. So the first time I write this code, I just store the name in a string variable called 'name'... nothing special. But when I rewrite this code I decide to break the name up into 'first' and 'last' names and replace the variable 'name' with two variables: 'first_name' and 'last_name'. Now if you are just accessing the data directly, you would have to change your code. Instead, because you are just using the 'GetName' function you no longer care how I've stored that name... just that you can access it through that one function.

That may seem like a pathetic example and that would be a fair assessment of it but hopefully it gets the point across.

Another important part of OO development is the idea of inheritance. So now we have another example...

Imagine a class called 'shape'. That class has a function called 'getArea'. I then write a 'circle' class that inherits from 'shape' and I write the 'getArea' function as required for the circle. I then write another class called 'rectangle' and it has its own version of 'getArea'.

Now when you go to code your use of these classes, you can just refer to these shapes as if they belong to the 'shape' class and not care about the actual implementation of the 'getArea' function. That is to say, whether you're looking at a 'circle' or a 'rectangle' is irrelevant as you know that they both have a 'getArea' function that takes the same inputs and gives the same outputs.

There are a number of other good reasons for OO development, but those two are the first that came to mind
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-14-2007, 01:56 PM Re: I still dont really gte the point of Classes...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Sorry, but i still dont get how i could use them

Ok look, i want to build a nice CMS and for instance i want it modulrised.
So how could i do this with classes how would it work?
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-15-2007, 07:49 AM Re: I still dont really gte the point of Classes...
Ultra Talker

Posts: 483
Trades: 0
It's kind of hard to just say "you would do it like this" because it depends on how you want to look at the CMS object model.

Firstly remember that there is nothing that can be done in OO that can't be done procedurally (which is how you are developing now). Also remember that it's not an "all or nothing" type approach.

If it were me, I'd have my users as objects at the very least. You may want your modules to be based on objects so that you can just loop through all of the modules and tell them to do whatever they want. In this case, you really benefit from not having to know what it is that the module may want to do... all you need to know is that you're going to call a particular function on that object.

I'm sorry it's all really vague, but it's one of those things that (for me, at least) is hard to explain. It's just a way of making your code model real life. Some people find it easier to work that way, some don't. If you don't, that's not a big problem.... it just means that you can do things like assembly really well, too (whereas I got less than 5% on my final exam: I still passed the course, but only barely).
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-15-2007, 07:59 AM Re: I still dont really gte the point of Classes...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Its just i what i have seen i think BUT WHY! like if i wanted to do it i could jsut make a couple of customer functions. and job done. but i been told it would be "messy" to do alot of the adnaced CMS stuff without classes
and also i would just genrally like to get more advanced..

It just seems a bit like with alot of things making it more complicated than it possibly needs to be. i just want to "get it" Lol
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-15-2007, 08:05 AM Re: I still dont really gte the point of Classes...
Ultra Talker

Posts: 483
Trades: 0
Like I say: some people think procedurally, some don't and either way it's not a bad thing.

I will tell you one thing: I always wrote procedurally until I went to university (where I learnt OO). Now, I don't know if it's because it was drilled into me for 4 years or because I actually had fantastic teachers, but since then I've been an OO man. That might be the problem here... you're asking people that just aren't great are extolling the virtues of it

One thing about OO is that as you start developing in a team, you will find lots of benefits as, like I said previously, you can change how your object does things without anyone else changing their code. It's hard to explain: hopefully someone else will give it a shot
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-15-2007, 08:58 AM Re: I still dont really gte the point of Classes...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Ok. i think i will give up trying to get it at the moment. ill do it how i would usually and try and code it that way to the best of my ability.

Maybe when i start taking it PHP in a more formal way (in college/uni) they can get it in my thick skull how to use it
i think i kind of get the idea of it and how its suposed to work

anyway never mind thanks for help
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-15-2007, 07:18 PM Re: I still dont really gte the point of Classes...
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
Lets start with cleaner code. Classes let you encapsulate a bunch of related functions with the data they work on. You could of course use a bunch of functions and pass data to them, return new data from them. But it is much much cleaner (and easier) to have a class that has all of the data self-contained. It also lets you hide certain data that isn't meant to be touched or used. For example, if you have a simple class Counter that counts upwards then the actual number that the counter holds shouldn't be changed. By using a class you can easily make variables private or protected.

But that is only one small part of why classes are useful. Two others are inheritance and polymorphism.

Inheritance lets you define a class that has "children" (subclasses) that inherit all of their properties and methods. For example, you have a "thing" in your program that can be of different types.

A practical example: Say your CMS has three different types of users. An Admin, an Editor and a User. You might define a base User class but extend the class for Admins and Editors to include additional (or different) functionality. You could do this procedurally but not efficiently. In your authenticate_user() function you'd need to include a conditional to see if the user is an admin and check if they have access to the ACP. What if 6 months down the line you decide to add a Super Editor or something? You need to go into every function with such conditionals and make another change. The more changes you have to make, the longer it will take and with better chances of introducing new bugs. If you do things in OOP then you just need to create a new subclass of user.

The next big feature of OOP is polymorphism. That is the ability to use different objects that are of different types as if they were of a single type. Going off of the User example above, each type of User shares common properties and methods.

For example, when logging in your code might call $User->authenticate(). It doesn't matter if the user is an admin, an editor or a normal user because all of the different types of users have an authenticate method. The different types of users might have dramatically different routines for authentication but it doesn't matter. Since you program to an interface, the code that uses your classes never needs to know which type it actually is.

Another popular example is with animals. You might have classes for a Dog, Cat and Bird the are children of a parent Animal. The Animal class has a method makeSound() that each child overrides. This allows you to create code that expects "any kind of animal". For example:
PHP Code:
$animals = array(new Dog, new Cat, new Bird);
foreach (
$animals as $animal) {
    
$animal->makeSound();

Using classes with inheritance and polymorphism gives you some real power in the design of your applications that you simply cannot get with procedural style programming.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 09-15-2007, 10:19 PM Re: I still dont really gte the point of Classes...
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
You can reuse code from older projects on newer projects
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 09-16-2007, 08:22 AM Re: I still dont really gte the point of Classes...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
I must seem so stupid. - because i am

I think im going to have to try and make them to understand them :/

Does anyone know a good (very very very very) simple Tutorial for making a CMS with Classes?

I hate this because i know once i get classes and how they can be used and stuff i would be making better code, but i just cant get it to work in my head Lol

Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-16-2007, 11:19 AM Re: I still dont really gte the point of Classes...
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Personally I don't think a lot of web stuff lends itself to classes very well. I'm an advocate of OOP, but when I create a CMS I don't use classes. Object Oriented Programming is just one style of programming used for certain kinds of problems. For example:

say you are programming a racing game, using oop gives you a strong organization scheme. For a simple game I would implement the following classes:

Game - the main class that encapsulates all of the other classes
Track - contains all of the important details about the track
Car - contains all of the important details about a car
Position - a class that keeps track of your current position (the game state)

If you really want to understand classes and oop learn Java, unlike PHP and C++, in java you can't help but use objects.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-16-2007, 11:22 AM Re: I still dont really gte the point of Classes...
johnncyber's Avatar
Extreme Talker

Posts: 216
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
I must seem so stupid. - because i am

I think im going to have to try and make them to understand them :/

Does anyone know a good (very very very very) simple Tutorial for making a CMS with Classes?

I hate this because i know once i get classes and how they can be used and stuff i would be making better code, but i just cant get it to work in my head Lol

Dan
My recommendation for you would be to find a Second Semester College Freshman Computer Science book (or at the very least a professors powerpoint presentations). The only thing is that you will be hard pressed to find a PHP CS book, but the books will teach you object oriented methodology.

EDIT: Check this PDF out: CS 1412 Lecture Notes (OO in C++)
__________________
~Mark Romero
-Co Founder | Tech Guru,
Please login or register to view this content. Registration is FREE

-FireFox Advocate,
Please login or register to view this content. Registration is FREE

Last edited by johnncyber; 09-16-2007 at 11:26 AM..
johnncyber is offline
Reply With Quote
View Public Profile Visit johnncyber's homepage!
 
Old 09-16-2007, 12:07 PM Re: I still dont really gte the point of Classes...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
thanks i will have to look at that when i have time
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Reply     « Reply to I still dont really gte the point of Classes...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.40473 seconds with 12 queries