Connecting Tech Pros Worldwide Forums | Help | Site Map

New to OO programming

gardnern
Guest
 
Posts: n/a
#1: Mar 8 '06
Ive been using PHP 4 for about 3.5 years now, and havnt messed with
objects and classes for whatever reason, however I know I need to start
using them. With someone who has no class/object programming
experience, php's manual is quite dull and leaves me scratching my
head. Is there a more indepth manual or tutortial somewhere that can
explain everything, and more importantly... I cant figure out why and
when an object/class would be used? Im also switching to PHP 5. Thanks.


Richard Levasseur
Guest
 
Posts: n/a
#2: Mar 8 '06

re: New to OO programming


OOP isn't a catch all for everything. For larger application
development it is a godsend, though.

PHP5 follows most of the OOP principles, and resembles java in many
ways. Any book, tutorial, information on Object Oriented Programming
will be relevent to PHP's Object model. There are only a few
exceptions between PHP5 and general OO models that i've run into in the
past (or more precisely, frustration at single inheritance)

Generally, however, objects are used when you have a lot of code that
is similar between projects or other entities in the project, such as
User's information, Database connections, Shopping carts, etc etc. All
those things are pretty much the same between projects, and can be
extended to do more for any specific application.

I wish i could provide a good resource for you, but i've yet to find
one myself.

Tony Marston
Guest
 
Posts: n/a
#3: Mar 9 '06

re: New to OO programming


Try http://www.tonymarston.co.uk/php-mys...seobjects.html and
http://www.tonymarston.co.uk/php-mys...eobjects2.html

For a more advanced version you can download and view the code in my sample
application at http://www.tonymarston.net/php-mysql...plication.html

You might also find
http://www.tonymarston.co.uk/php-mys...-heretics.html rather
interesting.

--
Tony Marston

http://www.tonymarston.net



"gardnern" <nathan@factory8.com> wrote in message
news:1141841964.202676.277930@v46g2000cwv.googlegr oups.com...[color=blue]
> Ive been using PHP 4 for about 3.5 years now, and havnt messed with
> objects and classes for whatever reason, however I know I need to start
> using them. With someone who has no class/object programming
> experience, php's manual is quite dull and leaves me scratching my
> head. Is there a more indepth manual or tutortial somewhere that can
> explain everything, and more importantly... I cant figure out why and
> when an object/class would be used? Im also switching to PHP 5. Thanks.
>[/color]


Scott
Guest
 
Posts: n/a
#4: Mar 9 '06

re: New to OO programming


I agree - the manual is pretty dull, especially when you don't
understand the whole purpose behind using OOP.

Richard was right when he said "For larger application development it is
a godsend, though." One reason why OOP is the way to go when developing
larger applications is because it allows you to focus on one particular
part of the application at a time, instead of trying to constantly keep
the whole big picture in your head.

For example, let's say you're developing a shopping cart. Think of the
cart itself as an object - a real world physical object, like a cart in
a grocery store. What purpose does the cart serve? It holds items. On
a website, a cart needs to be able to receive and remove items. So your
initial class would probably start out something like this (I'll do this
with PHP4 for now for the sake of simplicity):

class cart {

var $items = array(); // This will hold all the items in the cart

function addItem($itemNo, $qty) {
if(isset($this->items[$itemNo])) {
$this->items[$itemNo]['qty'] += $qty;
} else {
$this->items[$itemNo]['qty'] = $qty;
}
}

function removeItem($itemNo, $qty) {
if(isset($this->items[$itemNo])) {
$this->items[$itemNo] -= $qty;
}
}
}

Now, what you have is all the code that affects the shopping cart in one
place. You add properties and methods (vars and functions) based on what
functionality you want your cart to have. For example, you'll probably
want to add a displayContents() function for users to view the contents
of the cart.

The items you put in the cart can also be objects with properties such
as price, weight, item number, and name (although you'd need to do your
addItem function a little differently than I did). This will make it
easy to access these properties when you display the cart contents.
($this->items[$itemNo]->price) or something to that effect.

While PHP 5 has far superior OOP capabilities than PHP 4, it is also
much more complex. There's a book by W.J. Gilmore called "Beginning PHP
5 and MySQL: From Novice to Professional". While this wasn't the best
PHP book I've read, it did do a good job of explaining OOP in PHP 5.

Hope this helped!

Scott

gardnern wrote:[color=blue]
> Ive been using PHP 4 for about 3.5 years now, and havnt messed with
> objects and classes for whatever reason, however I know I need to start
> using them. With someone who has no class/object programming
> experience, php's manual is quite dull and leaves me scratching my
> head. Is there a more indepth manual or tutortial somewhere that can
> explain everything, and more importantly... I cant figure out why and
> when an object/class would be used? Im also switching to PHP 5. Thanks.
>[/color]
gardnern
Guest
 
Posts: n/a
#5: Mar 10 '06

re: New to OO programming


Thanks for all the replies, some great info here. Ill read through your
site Tony, and Ill be sure to check out that book Scott. I have W.J.
Gilmores book called "PHP and E-Commerce: From Novice to Professional"
and found it helpfull, so Im sure this one will too. Thanks again.

simonp@nospam.com
Guest
 
Posts: n/a
#6: Mar 10 '06

re: New to OO programming


Scott <nospam@nospam.com> wrote:[color=blue]
> I agree - the manual is pretty dull, especially when you don't
> understand the whole purpose behind using OOP.
>
>
> Hope this helped!
>
> Scott
>[color=green]
>>[/color][/color]

Excellent precis. Thank you.

Simon

--
Stupendous Tales
www.stupendoustales.com
Speculative Fiction, Pulp Dreams
Scott
Guest
 
Posts: n/a
#7: Mar 12 '06

re: New to OO programming


Glad you liked it. The benefits of OOP were a bit hazy to me until I
started thinking about it from the physical world aspect.

Scott

simonp@nospam.com wrote:[color=blue]
> Scott <nospam@nospam.com> wrote:
>[color=green]
>>I agree - the manual is pretty dull, especially when you don't
>>understand the whole purpose behind using OOP.
>>
>>
>>Hope this helped!
>>
>>Scott
>>
>>[/color]
>
> Excellent precis. Thank you.
>
> Simon
>[/color]
Closed Thread