NYCPHP Meetup

NYPHP.org

[nycphp-talk] Learning to use OOP in PHP

Francisco Reyes lists at natserv.com
Tue Mar 22 22:52:52 EST 2005


On Tue, 22 Mar 2005, Brian O'Connor wrote:
> I've been using PHP for about ~1 year now. and I feel I should be
> learning to use OOP to it's full capability in PHP.

Are you interestedin OOP or in code re-use?
Two different concepts.. just happens many people confuse them.
You can have code re-use without OO and have OO with little re-usability.

> with it a little bit, but the only real way I know how to use it for
> my purposes is to double query the database, once to get the ID, then
> do a while loop and create an instance of the class from that ID
> given.

I don't know others, but I would follow your sample much better if I had 
code to look at.

>...  I'm aware that
> pretty much anything you want to do on a webpage can be done without
> OOP, and strictly on the page, but I find myself writing a lot of the
> same code that has the exact same purpose on another page.

That's code re-use. You can achive it without using OO.

> to be able to keep my code organized, as well as be able to create a
> website for someone and have someone else maintain it with ease.

Which can be done without OO.

My advice is that you look for basic sites that explain Object Oriented 
programming and once you better understand the concept see if you think 
you like it and if it will be helpfull to you.

> Brother was attempting to explain to me how to do OOP without double
> querying the database, but it was over the phone and it was a bit more
> complicated

If you post the class you wrote myself and others would be able to give 
you feedback on your class.

> looking for are some tips on how to use it properly, or some
> recommended books / websites that explain this.  Thanks in advance.

I will try and give you my 2 cents version of what OO is.
It's a way to see operations based on items (or objects as used in the OO 
lingo).

Whereas before you would have functions like
format(string)
compute(number)

OO tells you to think of "objects" and how different operations can be 
performed on them.

Example you could have a class "figure" to refer to a geometrical figure.
You could think of what type of interaction you would need with such as 
figure such as finding it's area or volume.
In the non OO world you would have something like
area(figure)
volume(figure)

But how do you know the figure has a volume?
In OO you have
figure.area()
figure.volume()

The class itself can handle returning the proper value if there is a 
volume or perhaps 0 if the area is only 2 dimensions.

I am certain there are very decent explanations/tutorials out there in the 
net on OO. Once you understand the concept it is not very difficult to 
apply it to PHP.



More information about the talk mailing list