Reply to Re: Help understanding object-oriented approach

Your name:

Reply:


Posted by Rik on 02/22/07 17:29

On Thu, 22 Feb 2007 17:44:47 +0100, Andrew <Taylor> wrote:

> Hi,
>
> I've been using PHP for a long time, I have designed and developed a =

> number of mid-range systems. I've always used a procedural approach. I=
=

> fully understand the concept of OO, I know all the basics and I know h=
ow =

> to code OO. What I'm having problems with is understanding how to =

> design/architect projects using OO.
>
> Can anyone reccomend any good online tutorials / online learning / vid=
eo =

> tutorials which deal with these subjects specifically in PHP. I have a=
=

> number of the class OO books which help me understand the principals, =
=

> but, not how to leverage those principals in to system design.

Online I haven't found resources to my liking. I have thoroughly enjoyed=
=

the following book however: "PHP 5 Objects, Patterns, and Practice", by =
=

Matt Zandstra (#ISBN:1-59059-380-4).


> Now, we want to save this information in to a database, what is the =

> correct way (best practice) for doing this; is it to create a saveCD =

> method in my musicCD class? Should this method expect a database objec=
t =

> to be passed in, or, should it call static methods of another class?

I usually define either a Database as a singleton and/or save prepared =
=

statement resources as a private variable.

So it would be something like this (pseudocode):

class database{
private static $instance;
private $statements =3D array();
private function __construct(){
//some connection methods etc.
}
public static function getInstance(){
if(empty(self::$instance) self::$instance =3D new database;
return self::$instance;
}
public function getStatement($string){
if(!isset($this->statements[$string])) $this->statements[$string] =3D =
=

$this->createStatement($string);
return $this->statements[$string]
}
private function createStatement($string){
//code to get the prepared statement
return $statement;
}
}
class musicCD{
private $statements =3D array();
private $properties =3D array();
...
public function updateCD(){
if(!isset($this->statements['update'])){
$db =3D database::getInstance();
$this->statements['update'] =3D $db->getStatement('UPDATE `cds` SET =

`name` =3D :name,`length` =3D :length WHERE `id` =3D :id ');
}
$this->statements['update']->execute($this->properties);
}
}

Alternatively, you could prepare all statements in the __construct(), =

which would save you some typing database::getInstance() in every functi=
on =

that has to do something with the db, or you could declare your own =

private function int he class which does this. Might be some overhead =

preparing statements you're not going to need though.
-- =

Rik Wasmus

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация