NYCPHP Meetup

NYPHP.org

[nycphp-talk] Learning to use OOP in PHP

Brian O'Connor gatzby3jr at gmail.com
Wed Mar 23 11:09:52 EST 2005


Sorry for the vagueness.

Here is an example class I might write in order to retrieve
information for my news page:

class News

var $id;
var $rs;

  function News($id)
  {
     $this->id = $id;
     $query = "SELECT * FROM news WHERE id = $this->id;";
     $result = mysql_query($query);
     $rs = mysql_fetch_array($rs);
     $this->rs = $rs;
  }

  function getTitle()
  {
     return $this->rs['title'];
  }

  function getCategory()
  {
    return $this->rs['category'];
  }


etc with more methods almost identical to that
}

Also, it's not that I necessarily want to write OOP for everything,
but I don't like being lost when reading code with OOP in it.  I
understand some of it, but a lot of times certain things go right over
my head.

On Tue, 22 Mar 2005 22:52:52 -0500 (EST), Francisco Reyes
<lists at natserv.com> wrote:
> 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.
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
> 


-- 
Brian O'Connor



More information about the talk mailing list