开发者

PHP5 Object-Oriented Programming

I have started building a few applications using OO programing in PHP, however I am not sure If I am doing it the standard way.

Heres an example, if I had a book class

    class book{

            private $name;
            private $id;
            private $isbn;
    }

There are two scenarios, one, I want to add a brand new book to my database...

should I a) use a function within my new class to create the new book... ie.

    $book = new book;

    $book->addAsNew($name, $isbn);

Or should I B) have a function completely independent of the class that adds a new book?

Secondly.. when opening my book class, should I have A) a constructor

    function __construct( $bookId ){

            //Call mysql DB and set $name and $isbn var based on $bookId

    }

    ...

    $book = new book( $bookId );

of should I b) have a separate function..

    class boo开发者_如何学运维k{

            private $name;
            private $id;
            private $isbn;

            public initiated = 0;

            function initiate( $bookId ){

                    //Load $name and $isbn from DB based on $bookId

                    $initiated = 1;
            }
    }

    ...

    $book = new book;

    $book = initiate( $bookId );

Is there a standard way most programmers would do this? or is it just mainly at the discretion of the programmer?


You can add parameters to the constructor too, so you could write

book = new Book($name, $isbn). 

But in this case, I'd choose to have a class with separate properties, like you have, and create a separate factory class or function to instantiate the book objects.

The same goes for B. Don't put the DB queries in this class. Create a separate BookData object that can act as a factory. It can query the database and return the book object(s). Of course, if you feel a factory class is over complicated, you can create a function too, but hey, you wanted to go OO. ;-)


What you are doing is creating models and data mappers, please refer to:

  1. http://www.devshed.com/c/a/PHP/Implementing-the-Data-Mapper-Design-Pattern-in-PHP-5/
  2. http://www.doctrine-project.org/ - http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/getting-started-xml-edition.html

So your book class is a model, and that model needs to be stored somewhere. Pseudo-code would be:

$book = new Book();
$book
    ->setName($name)
    ->setId($id)
    ->setISBN($isbn);

$bookDataMapper = new BookDataMapper();
$bookDataMapper->save($book);


You could do both!

Calling initiate from the constructor

function __construct( $bookId = null) {
   if ($bookId) {
      $this->initiate($bookId);
   }
}

From a OOP perspective this is nice, because it prevents an empty object.

empty object: An object that issn't a real live object yet, but exists only in code.

But because the $bookId parameter is optional, an empty object is still possible, allowing you to create new book(record)s with the book class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜