Do you do OOP?

Hi…

Could you post a couple of (near) duplicates? I can comment on these. It’s easier on a forum to deal with concrete problems first. makes it easier to establish a shared understanding before going on to the abstract stuff.

Give me an example of a problem you have right now.

I can’t give you a general answer to this as it depends very much on the problem. I can give you some mechanics to deduce this yourself. Firstly I need a problem :).

This is the kiss of death. Whatever you do, don’t try to design for projects that don’t exist yet.

On the other hand, if you already have multiple projects then the process is easy. Write one, then start writing the other. Pull out common code and then plug the common code back in using a “flex point”. Adding flex points is something that OO is extremely good at, and well, that’s kind of the whole point. Even fiendishly difficult situations have known solutions (called “design patterns”).

Anyways, that’s for the future. Right now you have a specific problem, yes?

yours, Marcus

Hi Marcus

another lonely programmer here, trying to get my classes organised and I have a real example.

I’m rewriting a procedural ecommerce site to use oop. On the home page i want to display promotions - a PromoPanel for each promotion, and within that an ItemPanel for each product. The item panels can be fullwidth (uses class ItemPanel1) or 2 across (ItemPanel2). The display panels are already classes, but the data handling is all procedural.

Tables used are:
promotions : id, start&end date, title, layout (1 or 2 - to determine which itempanel to use), and a few other styling fields

promoprodidx : promotionid, productid

What i am doing at the moment is:
query current promotions -> promoarray
foreach promoarray,
create PromoPanel
query product details from promoprodidx & product table -> productarray
pass productarray to PromoPanel
within PromoPanel, use a switch to determine layout & foreach productarray create ItemPanel1 or ItemPanel2

Would love some advice on how to tidy this all up!

www.cakephp.org worked for me.

Hi…

That’s quite a lot of stuff :).

As this type of operation is often repeated for most web sites, what you are asking me for is the design of a web framework. There are lot’s of ways of doing this:

  1. Use a full stack MVC framework such as Symphony or Cake. There are plenty to choose from.

  2. Roll your own from pieces, such as Zend controllers, ezComponents, Doctrine ORM, etc, etc.

  3. Write some parts of this yourself, using a few libraries.

The essential division is the separation of the DB stuff from the page oriented stuff. What’s left should be the minimum of glue code. It’s the glue code that gets messy and is difficult to test. It’s also the glue code that captures the design of the app, so you want it uncluttered.

For the data we choose an ORM. You want to be able to write code like this…


foreach ($products->promotions() as $promotion) {
    $layout_class = $promotion->layout == 'wide' ? 'FullWidth' : 'TwoColumn';
    foreach ($promotion->items as $item) {
        $layout = new $layout_class($item);
        ...
    }
}

Probably not quite like this as this code would be spread over the pieces that paint the HTML, but this shows the interface to the data.

$products is a “Repository” of data stuff. It holds the database connection and a single instance is created by the glue code. That should be the only database object that the glue code creates directly.

$products->promotions() is probably something like a result set iterator, but one that wraps the row in a Promotion class as it returns each one.

$promotion->layout is just a field in the $promotion.

$promotion->items will trigger a method call to fetch the items from the database. Again, each row will be wrapped in a PromotionItem class.

ORM (object relational mappers) do all of this for you. Checkout Doctrine and Propel, and most frameworks use these or have their own. The pattern is called “Active Record”. There are simpler solutions for purely tabular data, but when you want lots of little UI objects it’s a great pattern.

I wouldn’t write this stuff yourself unless you have a masochistic streak. It will take weeks to get both transactions and performance working together. Also most ORM tools include many optimisations. For example, instead of sending one query for the promotions and then a query for each item, most ORMs will send just one query for all of the subparts. That is two queries overall.

On the front end you can write code like this…


<?php
...
$page = new MyPageController();
?>
<html>
<head><title><?= $page->title ?></title></head>
...

That is, page oriented code. Dead easy and a natural choice if you are rolling your own.

Or you can route everything through index.php and have one controller, called the “Front Controller” do all the routing. Most frameworks are FrontController based, but there is a learning curve associated with this and you can’t mix and match different systems.

Once all that mechanical stuff is separated out, it’s easier to write your application. That goes into the controller for now. I’m simplifying here, as often you have an additional layer between the controller and the ORM. Sometimes this is explicitly called the “Model” or “Business Logic”. Worry about that later.

The best way to study front end patterns is to use a framework for a bit. The ORM stuff is best learned by trying to write a simple one in your spare time.

I’m not sure this is the answer you want, but it’s difficult to go into more detail without writing a few hundred lines of code. If you ask a follow on question, we can dig deeper.

yours, Marcus

Hmm…its kinda hard to explain a problem sometimes.
But generally, i find myself copy and pasting codes and modifying a few inputs. i generally find this faster than thinking of how to build a flexible class. in the end, i end up with a large chunk of codes which i have to change if 1 changes, which is extremely frustrating cause they may span across many files.

I do have problems remembering file names and variables that I created, any suggestions on that?

head first OOA&D did a great explanation for me. however, i think its easier to refactor codes into OO rather than do it the first time? Is this the right approach? Or do you guys, do a whole lot of UML like use cases, class diagrams, sequence diagrams etc before coding?

Another thing, when they say build codes that are reusable. Does it refer to the current project, or does it refer to other projects that may use it?

zend framework is how you should not program OOP.

Its just a bunch a static classes that cause spaghetti code.

I don’t know about spaghetti but statics are not evil, their abuse is. Likewise for singletons, if you used singletons to limit the instances of a class to one, because anymore would cause issues and you did this in say a single cetralize location, than singletons make a lot of sense and thus so do statics.

If you abuse singleton by calling it’s static Class::getInstance() all over your code because you need it’s object and are to lazy to inject the registry or whatever instead, then singletons suddenly become bad practice.

Zend IMHO has the greatest focus on IoC than any other framework or codebase, so statics or not, it’s by far the most flexible to use as a library.

It’s bloated, over engineered implementation can at times bug me, but I’d rather that than concrete implementations like that of CodeIgnitor, etc.

Cheers,
Alex

Thanks for that marcus. I understand the theory, just need to do a lot more reading now. I don’t really understand some of the terminology.

Was going to post a request for a good book to explain it all and noticed the link in your signature. PHP in action sounds like it was written for me!. My only concern is that it’s 2 years old. Presumably, things haven’t changed too much? same concepts, right?

Unfortunately, too late for the ’ don’t write it all yourself’ warning - have spent weeks and weeks on the site so far.

Thanks again for your time.

I was never able to code an application of any measurable complexity until I started using OOP, why? Simple, because without, I found I needed to be aware of all things, all the time.

With OOP, I just need to code small to build big. Small methods, in small objects in small modules and so forth…

I know this is an old thread but I think it’s worth bumping.

I was going to learn PHP just using functions but I’ve decided to learn it the OOP way so I don’t end up having to teach myself all over again.

I have currently been teaching myself the following way:

  • Read books/articles about general OOP theory.
  • Read books/articles on PHP specific OOP.
  • Checking out different frameworks written in PHP (currently reading about Yii and I really like how it all works).
  • Writing code I see in different books/articles. I find that writing it out and not just reading about it helps my understanding somehow.
  • Starting to write some basic classes and hesitantly working on a mysql db connection script that also outputs and a form validation script.
  • Taking a break doing some xhtml/css and reading a good penguin classic :wink:

It’s a bit of a process, however I’ve found it infinitely rewarding :slight_smile:

I used c++, c# and Java before PHP. You should start with basic oo concepts and after that you should read and understand design patterns. If you don’t know and understand design patterns you can not say you really write oo code.

Hi…

Get “Patterns of Enterprise Application Architecture” by Martin Fowler. It’s the root of a lot of pattern names. You don’t have to read the whole book as it’s a catalogue.

yours, Marcus