Classes in PHP

This is a discussion on "Classes in PHP" within the PHP Forum section. This forum, and the thread "Classes in PHP are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Oct 11th, 2007, 10:55
SuperMember

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Classes in PHP

Hi

I have never used Classes in php, are they easy to use and can anybody reccomend a good site for learning them.

I have a file called functions that has all my repeatable code in it. Under what circumstances would I use a class?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 11th, 2007, 11:01
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

Classes are the BOMB! They are the way of the future with PHP...but I'm a bit biased

I actually wrote a tutorial in Webforumz newletter last month that should be a good start. If you have PHP 5, it gets even better.

They are very easy and generally the question is "When shouldn't I use classes?" and there aren't many answers.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 11th, 2007, 11:06
SuperMember

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

Thanks

I have only been a member for a very short while but I am so impressed at the speed with wich some of these threads are answered. And most of the time with good answers with people actually reading the question.

Will have a read of the newsletter and get back to you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 11th, 2007, 13:35
SuperMember

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

Hi Rakuli

Just finished reading your article on OO and classes.

I found the first part straight forward to follow and very interesting since I have often wondered what certain caracters were when reading other peoples code.

I can see how I would create a class and call it but still trying to understand why this is a better method. Maybe I need to put it into action. I found the second part informative but much harder to follow.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 11th, 2007, 13:37
SuperMember

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

And something else I forgot to mention:

You have not explained how this would work relative to file structures, would the classes sit in different files and if so any particular area?

Would you have lots of classes for different things?

Would these class files then be incliuded at the top of my content php page?

Can you tell I am new to this??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 11th, 2007, 13:41
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

The main think is to keep your PHP work separate from the output..

Did you have a look at the testing page I set up http://www.openthource.com/abstraction.php

It has a few examples of how the object can be set up to do the work and jsut give the results you need.

Also, take a look at the [url=http://prea.php.net]PHP Extension and Application Repository (PEAR)[/php] which has tonnes of pre-built classes. Using these will give you an idea of their power.

You just include them into your script, pass them a value and they do all the work.

Feel free to ask about the concepts though if you struggle.. Getting your hands dirty is a good start, working through with the tutorial may help.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)

Last edited by Rakuli; Oct 11th, 2007 at 14:01.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Oct 11th, 2007, 13:56
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

Quote:
Originally Posted by parms View Post
And something else I forgot to mention:

You have not explained how this would work relative to file structures, would the classes sit in different files and if so any particular area?

Would you have lots of classes for different things?

Would these class files then be incliuded at the top of my content php page?

Can you tell I am new to this??
A class can be anywhere, but it is convention (and helps in organisation) to have each class in its own file. You can have a class intermingled with your code but this sort of defeats the purpose.

Yes, you have many classes. Say one to deal with users and a class for products that lets you create an object for each product.

If you had an online shop, you could have a class that creates objects for each item. Each item has properties (price, name, availability) and methods (changePrice, checkAvailability).

Once you have set up the methods, you can call them and they take all of the processing out of the script that's outputting their data......

Ummmm.. an anolgy for OOP abstraction is this.

Your computer hardware is the base of your computer heirachy the next level up in this abstraction is the operating system kernel , then the hardware device drivers followed by applications running on your operating system.

When you write a computer program, it would be very hard to have to write the code to deal with the hardware primitives for everything. Instead, the program takes advantage of object libraries in the operating system (which in turn deals with the drivers objects that deal with the hardware) allowing the end program to just asks for those results.

This is why programs generally cannot be used on different operating systems as they are tapping into the objects set up for a given system. The program doesn't have to worry about the fiddly bits and can just use the results of the abstraction to perform its functions.

I hope I am making sense. OOP doesn't allow you to do anything more than what you can do normally, it just allows you to do it in a more ergonomical, logical way.

It's a way of mind

*** Rakulie stops himself before saying "very good grasshopper" ***
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)

Last edited by Rakuli; Oct 13th, 2007 at 23:15.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Oct 11th, 2007, 15:04
SuperMember

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

Okay, on my next project I may start implementing the odd class here or there. I will let you know as I start planning this, if thats okay.

I will summarise what I am doing and how I intend to do it. Then would be nice to have a few pointers on where to consider applying classes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Oct 11th, 2007, 15:34
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Classes in PHP

I always use OOP.
I love it!
Keeps everything nice and organized.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Oct 11th, 2007, 16:00
Junior Member
Join Date: Oct 2007
Location: Louth
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

I also have little problem with using OOP. Prefer for simple task to use more simple script writing.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Oct 11th, 2007, 16:52
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Classes in PHP

Quote:
Originally Posted by parms View Post
I will summarise what I am doing and how I intend to do it. Then would be nice to have a few pointers on where to consider applying classes.
Make an organized library.
You can either have all your classes is one file,
or have them in different files and include a file called library which includes all the individual files.
Hope that makes sense.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Oct 13th, 2007, 17:24
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Classes in PHP

Cons:

1) Classes/OOP are unnecessary overhead in some cases. IMO it is overused.

2) You can do quite a bit of the organization work done in OOP by using include()/require() features. You don't have to use a class to reuse code on multiple pages.

3) There is fairly hefty amount to learn.

Pros:

1) The overhead cost becomes relatively smaller and smaller as multiple pages use the same pieces of code, multiple people work on the script, or as the sheer size and complexity of a page script grows.

2) If you want to do PHP contract work for a big outfit, or get a job for someone else, you have to learn how to use OOP just because other people are going to insist on it.

3) Even if it is unnecessary, it forces you into certain degree of organization. In other words, you might not actually need OOP, but implementing it requires you to do the organizational work you should be doing anyway.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
variable classes CloudedVision PHP Forum 4 Feb 14th, 2008 03:07
Using Classes tox0tes Flash & Multimedia Forum 1 Nov 9th, 2007 14:12
Classes pa007 PHP Forum 6 Jul 12th, 2007 15:22
Classes Gee Bee JavaScript Forum 1 Mar 8th, 2006 08:31
Styles in classes? timmytots Web Page Design 4 Dec 2nd, 2005 01:20


All times are GMT. The time now is 06:29.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42