PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > first php 5 class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
first php 5 class

Réponse
 
LinkBack Outils de la discussion
Vieux 29/01/2008, 20h19   #1
nihilism machine
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut first php 5 class

Ok, trying to write my first php5 class. This is my first project
using all OOP PHP5.2.5.

I want to create a config class, which is extended by a connection
class, which is extended by a database class. Here is my config class,
how am I looking?

<?php

class dbconfig {
public $connInfo = array();
public $connInfo[$hostname] = 'internal-db.s23499.gridserver.com';
public $connInfo[$username] = 'db23499';
public $connInfo[$password] = 'ryvx4398';
public $connInfo[$database] = 'db23499_donors';

public __construct() {
return $this->$connInfo;
}
}

?>
  Réponse avec citation
Vieux 29/01/2008, 20h29   #2
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] first php 5 class

On Jan 29, 2008 3:19 PM, nihilism machine <nihilismmachine@gmail.com> wrote:

> Ok, trying to write my first php5 class. This is my first project
> using all OOP PHP5.2.5.
>
> I want to create a config class, which is extended by a connection
> class, which is extended by a database class. Here is my config class,
> how am I looking?
>
> <?php
>
> class dbconfig {
> public $connInfo = array();
> public $connInfo[$hostname] = 'internal-db.s23499.gridserver.com';
> public $connInfo[$username] = 'db23499';
> public $connInfo[$password] = 'ryvx4398';
> public $connInfo[$database] = 'db23499_donors';
>
> public __construct() {
> return $this->$connInfo;
> }
> }
>
> ?> <http://www.php.net/unsub.php>



if youre going to have a class for configuration information; you probly
should
go for singleton:
http://www.phppatterns.com/docs/desi...rn?s=singleton

-nathan

  Réponse avec citation
Vieux 29/01/2008, 20h32   #3
Jochem Maas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] first php 5 class

nihilism machine schreef:
> Ok, trying to write my first php5 class. This is my first project using
> all OOP PHP5.2.5.
>
> I want to create a config class, which is extended by a connection
> class, which is extended by a database class. Here is my config class,
> how am I looking?


dunno can't see you. but your class looks like crap, in fact it don't think it
will even parse. have you tried running it?

>
> <?php
>
> class dbconfig {
> public $connInfo = array();
> public $connInfo[$hostname] = 'internal-db.s23499.gridserver.com';
> public $connInfo[$username] = 'db23499';
> public $connInfo[$password] = 'ryvx4398';
> public $connInfo[$database] = 'db23499_donors';


the above is plain wrong.

1. you can't do multiple property definitions for a single [array] property
2. your storing hardcoded values in a class which is meant to be somewhat generic/reusable
3. you've just told the world your password/login/db credentials

>
> public __construct() {
> return $this->$connInfo;
> }


constructors aren't meant to return anything. besides you won't be able to
retrieve the returned value.

not too mention '$this->$connInfo' is the wrong syntax it should be:

$this->connInfo

I'd recommend some more research on basic class syntax.

> }
>
> ?>
>

  Réponse avec citation
Vieux 29/01/2008, 20h42   #4
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: first php 5 class

On Jan 29, 3:19 pm, nihilismmach...@gmail.com (nihilism machine)
wrote:
> Ok, trying to write my first php5 class. This is my first project
> using all OOP PHP5.2.5.
>
> I want to create a config class, which is extended by a connection
> class, which is extended by a database class. Here is my config class,
> how am I looking?
>
> <?php
>
> class dbconfig {
> public $connInfo = array();
> public $connInfo[$hostname] = 'internal-db.s23499.gridserver.com';
> public $connInfo[$username] = 'db23499';
> public $connInfo[$password] = 'ryvx4398';
> public $connInfo[$database] = 'db23499_donors';
>
> public __construct() {
> return $this->$connInfo;
> }
>
> }
>
> ?>


A few things.

First, you shouldn't return anything from __construct() as it won't
make it back to the caller anyway. The constructor is used to
initialize the object. The "new" operator will take care of assigning
it to a variable. In other words, when you write:

$foo = new Bar();

$foo isn't getting the return value of Bar's __construct() method. It
gets the instance of the object that was just created.

Next, think about inheritance this way: the child class should be a
more specific version of the parent class. So, while a database class
might use your connection parameters it isn't really a more specific
version of "dbconfig." Same goes with "connection" -- is it really a
more specific version of "config?" Is "database" really a more
specific version of "connection?"

Additionally your classes should be reusable. This means that the
database class itself shouldn't be bound to specific connection
parameters. A particular instance of the database class can be,
however. Passing these to the constructor is a reasonable thing to
do. So your database class might look like this:

class Database {
protected $hostname;
protected $username;
protected $password;
protected $database;

protected $conn;

public __construct($hostname, $username, $password, $database) {
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}

protected function openConn() {
$this->conn = db_connect($this->hostname, $this->username,
$this->password, $this->database);
}
}

And you would instantiate it like this:

$db = new Database('internal-db.s23499.gridserver.com', 'db23499',
'ryvx4398', 'db23499_donors');


Now for your "config" class. You shouldn't expose the actual storage
method of the parameters to the outside world -- so all his members
should be private or protected. That way, if you change the way you
store them (maybe they each get their own member variables or you get
that at runtime from somewhere else) your other classes won't care.

I would write it like this:

class Config {
protected $hostname = 'internal-db.s23499.gridserver.com';
protected $username = 'db23499';
protected $password = 'ryvx4398';
protected $database = 'db23499_donors';

public function getHostname() {
return $this->hostname;
}

public function getUsername() {
return $this->username;
}

public function getPassword() {
return $this->password;
}

public function getDatabase() {
return $this->database;
}
}

Now you can create your database like this:

$conf = new Config();
$db = new Database($conf->getHostname(), $conf->getUsername(), $conf-
>getPassword(), $conf->getDatabase());


Another way would be to write your database class so that it takes the
config in the constructor instead of each individual parameter:

class Database {
protected $hostname;
protected $username;
protected $password;
protected $database;

protected $conn

public __construct(Config $conf) {
$this->hostname = $conf->getHostname();
$this->username = $conf->getUsername();
$this->password = $conf->getPassword();
$this->database = $conf->getDatabase();
}

protected function openConn() {
$this->conn = db_connect($this->hostname, $this->username,
$this->password, $this->database);
}
}

Then you'd make a new database like this:

$conf = new Config();
$db = new Database($conf);

Alternatively the database could just hold on to the config object
itself:

class Database {
protected $conf;

public __construct(Config $conf) {
$this->conf = $conf;
}

protected function openConn() {
$this->conn = db_connect($this->conf->getHostname(), $this-
>conf->getUsername(), $this->conf->getPassword(), $this->conf-
>getDatabase());

}
}

Hopefully that's all clear as mud. Let us know if you need any
clarification.
  Réponse avec citation
Vieux 30/01/2008, 13h40   #5
Eric Butera
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] first php 5 class

On Jan 29, 2008 3:29 PM, Nathan Nobbe <quickshiftin@gmail.com> wrote:
> On Jan 29, 2008 3:19 PM, nihilism machine <nihilismmachine@gmail.com> wrote:
>
> > Ok, trying to write my first php5 class. This is my first project
> > using all OOP PHP5.2.5.
> >
> > I want to create a config class, which is extended by a connection
> > class, which is extended by a database class. Here is my config class,
> > how am I looking?
> >
> > <?php
> >
> > class dbconfig {
> > public $connInfo = array();
> > public $connInfo[$hostname] = 'internal-db.s23499.gridserver.com';
> > public $connInfo[$username] = 'db23499';
> > public $connInfo[$password] = 'ryvx4398';
> > public $connInfo[$database] = 'db23499_donors';
> >
> > public __construct() {
> > return $this->$connInfo;
> > }
> > }
> >
> > ?> <http://www.php.net/unsub.php>

>
>
> if youre going to have a class for configuration information; you probly
> should
> go for singleton:
> http://www.phppatterns.com/docs/desi...rn?s=singleton
>
> -nathan
>


Still pimping singleton, huh?
  Réponse avec citation
Vieux 30/01/2008, 14h57   #6
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] first php 5 class

On Jan 30, 2008 8:40 AM, Eric Butera <eric.butera@gmail.com> wrote:

> On Jan 29, 2008 3:29 PM, Nathan Nobbe <quickshiftin@gmail.com> wrote:
> Still pimping singleton, huh?
>


hell yeah

i looked at the registry classes you pointed out.
you know what funny about the one in solar?
they refer to the same article i mentioned, namely,
the article at the phppatterns site. good to know
somebody else out there thinks highly of that site

also, i took a look (briefly though), in patterns of
enterprise architecture by martin fowler, which is
(perhaps) where the pattern was originally formalized.
he says that he prefers to have the data in the registry
stored in instance variables, but obviously there are ways
to vary the implementation of a pattern. ergo, in his
design the registry class itself would be a singleton.

it looks like the 3 examples you showed previously, are
all of essentially the same design. a class uses static
class members to store the data, and it is essentially
global, because well, any bit of code can reference the class.
id say the technique only works as a consequence of phps
dynamic nature, that is, in other languages like java and c++
i dont think you can create static class members on the fly.
the technique is certainly interesting.

-nathan

  Réponse avec citation
Vieux 30/01/2008, 15h35   #7
Eric Butera
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] first php 5 class

On Jan 30, 2008 9:57 AM, Nathan Nobbe <quickshiftin@gmail.com> wrote:
> On Jan 30, 2008 8:40 AM, Eric Butera <eric.butera@gmail.com> wrote:
>
> >
> >
> > On Jan 29, 2008 3:29 PM, Nathan Nobbe <quickshiftin@gmail.com> wrote:
> > Still pimping singleton, huh?
> >

>
> hell yeah
>
> i looked at the registry classes you pointed out.
> you know what funny about the one in solar?
> they refer to the same article i mentioned, namely,
> the article at the phppatterns site. good to know
> somebody else out there thinks highly of that site
>
> also, i took a look (briefly though), in patterns of
> enterprise architecture by martin fowler, which is
> (perhaps) where the pattern was originally formalized.
> he says that he prefers to have the data in the registry
> stored in instance variables, but obviously there are ways
> to vary the implementation of a pattern. ergo, in his
> design the registry class itself would be a singleton.
>
> it looks like the 3 examples you showed previously, are
> all of essentially the same design. a class uses static
> class members to store the data, and it is essentially
> global, because well, any bit of code can reference the class.
> id say the technique only works as a consequence of phps
> dynamic nature, that is, in other languages like java and c++
> i dont think you can create static class members on the fly.
> the technique is certainly interesting.
>
> -nathan
>


The only gripe I have about the registry pattern is the lack of code
completion in PDT.

I love php patterns, but it seems to sort of be dead for years now.

I just keep downloading copies of various frameworks and poke through
their implementations. At the end of the day though it is my job to
write code, so I'm going to always balance the code purity vs getting
it done. I am okay with a registry that uses static methods because
it works in my projects. Some people would insist on being able to
create an instance and pass that around, but I don't need that level
of complexity.
  Réponse avec citation
Vieux 30/01/2008, 16h13   #8
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] first php 5 class

On Jan 30, 2008 10:35 AM, Eric Butera <eric.butera@gmail.com> wrote:

> The only gripe I have about the registry pattern is the lack of code
> completion in PDT.



heh. does that still blowup when you try to open a file w/ an interface
definition?


> I love php patterns, but it seems to sort of be dead for years now.



me too; ya, it is sort of dead, sad, but its still worth a look to people
getting
there feet wet w/ patterns, and occasionally as a point of reference for
patterns implemented in php.


> I just keep downloading copies of various frameworks and poke through
> their implementations.



this is a great idea, and youve got me doing more of it.


> At the end of the day though it is my job to
> write code, so I'm going to always balance the code purity vs getting
> it done. I am okay with a registry that uses static methods because
> it works in my projects.



its sorta like the 'php way' for the registry. theres something we can say
the java guys cant do

Some people would insist on being able to

> create an instance and pass that around, but I don't need that level
> of complexity.



i dont think Registry::getInstance() is really that much overhead; but it is
another line of code to write

-nathan

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h02.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,54241 seconds with 16 queries