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.
|
|
|
|
|
![]() |
||
Classes in PHP
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
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? |
|
|
|
#2
|
||||
|
||||
|
Re: Classes in PHP
Classes are the BOMB!
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)
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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?? |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
Re: Classes in PHP
Quote:
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. |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
||||
|
||||
|
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)
|
|
#10
|
|||
|
|||
|
Re: Classes in PHP
I also have little problem with using OOP. Prefer for simple task to use more simple script writing.
|
|
#11
|
||||
|
||||
|
Re: Classes in PHP
Quote:
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)
|
|
#12
|
||||
|
||||
|
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. |
![]() |
| Thread Tools | |
|
|
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 |