FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » OOP, classes and databases
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
OOP, classes and databases [message #169408] Fri, 10 September 2010 13:18 Go to next message
Mattias Campe is currently offline  Mattias Campe
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Hello

I'm programming some classes that get their information from a MySQL
database. I started with mysql_query, function X () as constructor and
getValue and setValue.

I already found out that:
- PDO is better than mysql_query
- __construct is the way of making constructors
- __get and __set is the way for getting and setting properties


But I'm still kind of stuck with the 'best practices' with working
together with classes and databases. For now I have a class, let's say
'Person', that kind of works like this:

class Person {
private $name;
private $address;

public function __construct ($id,$db) {
$query = ... the select query ...
$row = $db->query($query);
foreach ($db->query($query) as $row) {
$this->name = $row['name'];
$this->address = $row['address'];
}
}

public function __get ($var) {
switch ($var) {
case 'Name':
return $this->name;
break;
case 'address':
return $this->address;
break;
default:
break;
}
}

}

But I have the feeling that this isn't a 'best practice':
- I can't use this class without database
- should I put the select query and getting the data in a seperate
function, outside the constructor?
- should I pass the database handler as a parameter or should I do it in
another way?


Would somebody happen to know a website that explains a little bit more
about OOP, classes and databases? I've already tried to do it myself,
but for now, I can't find the desired result.

Greetings
Mattias
Re: OOP, classes and databases [message #169414 is a reply to message #169408] Fri, 10 September 2010 19:48 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
Mattias Campe schrieb:

> - PDO is better than mysql_query
Yes. And use prepared statements.

> - __construct is the way of making constructors
Yes.

> - __get and __set is the way for getting and setting properties
No. Usually properties are private and you write getFoo() and setFoo(
$value ) methods to retrieve and set their values in a controlled way
(allowing to validate the value to be set, for example). There may be
circumstances where it is okay to allow anyone unrestricted access to a
property. In this case, you can make the property public and don't need
__get/__set. If you want to control access to a property, but still
allow the simpler syntax $object->foo = 42, then you can use
__get/__set. Also, if you want to allow public read-only access to a
private property, you can use __get to achieve that.

> class Person {
> private $name;
> private $address;
>
> public function __construct ($id,$db) {
> $query = ... the select query ...
> $row = $db->query($query);

That last line above is not needed as you re-do the same query in the
foreach loop below.

> foreach ($db->query($query) as $row) {
> $this->name = $row['name'];
> $this->address = $row['address'];

And anyway, that query should return at most one result (it is about one
specific person, isn't it?), so there's no need for the loop. You could
just do:

public function __construct( $id, PDO $db )
{
// Create a prepared statement (the ? is a placeholder)
$stmt = $db->prepare( 'SELECT * FROM persons WHERE Id = ?' );
// Execute that statement replacing the ? with the given $id
$stmt->execute( array( $id );
// Retrieve the one expected row as an object
$row = $stmt->fetchObject();
if ( $row ) // If the query succeeded
{
$this->name = $row->name;
$this->address = $row->address;
}
else // If the record could not be found
{
echo 'There is no person with this id in the database.';
}
}

Note: This code is untested and may even contain syntax errors or other
typos. It is just meant for illustration.

> public function __get ($var) {
> switch ($var) {
> case 'Name':

You mean lowercase 'name' here.

> return $this->name;
> break;
> case 'address':
> return $this->address;
> break;
> default:
> break;

This could be simplified like this:

public function __get( $var )
{
return isset( $this->$var ) ? $this->$var : null;
}

Note: This would allow public read-only access to *all* properties. To
restrict access to just certain properties, you could use this:

public function __get( $var )
{
return in_array( $var, array( 'name', 'address' ) )
? $this->$var
: null;
}

> But I have the feeling that this isn't a 'best practice':
> - I can't use this class without database

You could pass it an object which has the same methods as the PDO
object, but retrieves data in a completely different way (like reading
it from a text file or from an array). What you did is called dependency
injection and is actually indeed a 'best practise'. On the other hand --
since your Person class seems to be just a simple data container, you
might as well do it the other way round and tell PDO to give you back
the data in form of a Person object:

$stmt->fetchObject( 'Person' );

But then you would need to make the properties public, so PDO can access
them to put the values there (and remove all arguments from the
constructor as they would not be needed). In this scenario, you would
not pass the PDO object to the Person constructor, but pass the Person
class to the PDO statement instead.

> - should I put the select query and getting the data in a seperate
> function, outside the constructor?

You could do it the way I suggested above. In any case, the name and
address properties must be set when the Person object is created. You
must not allow a Person object to exist without having its name and
address set!

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: OOP, classes and databases [message #169415 is a reply to message #169414] Fri, 10 September 2010 19:55 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
Thomas Mlynarczyk schrieb:
> $stmt->execute( array( $id );
Found a typo: missing ).

> else // If the record could not be found
> {
> echo 'There is no person with this id in the database.';
> }

Even better, throw an exception. Otherwise you would end up with a
Person having neither name nor address -- and that must not happen.

Greetings,
Thomas


--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: OOP, classes and databases [message #169418 is a reply to message #169414] Sat, 11 September 2010 13:45 Go to previous messageGo to next message
aaaa is currently offline  aaaa
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
>> - __get and __set is the way for getting and setting properties
> No. Usually properties are private and you write getFoo() and setFoo(
> $value ) methods to retrieve and set their values in a controlled way
> (allowing to validate the value to be set, for example). There may be
> circumstances where it is okay to allow anyone unrestricted access to a
> property. In this case, you can make the property public and don't need
> __get/__set. If you want to control access to a property, but still allow
> the simpler syntax $object->foo = 42, then you can use __get/__set. Also,
> if you want to allow public read-only access to a private property, you
> can use __get to achieve that.

Methods __get and __set are OK, if you use them like this:

public function __set($var, $val)
{
switch ($var)
{
case 'name1' : //validation here
break;
case 'name2' :
case 'name3' : //validation here
break;
}
$this->variables[$var] = $val;
}

--
A
Re: OOP, classes and databases [message #169436 is a reply to message #169408] Mon, 13 September 2010 06:41 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 10/09/2010 15:18, Mattias Campe escribió/wrote:
> public function __construct ($id,$db) {
> $query = ... the select query ...
> $row = $db->query($query);
> foreach ($db->query($query) as $row) {
> $this->name = $row['name'];
> $this->address = $row['address'];
> }
> }
[...]
> But I have the feeling that this isn't a 'best practice':
> - I can't use this class without database
> - should I put the select query and getting the data in a seperate
> function, outside the constructor?

You are right about this gotcha. One approach is to make your
constructor and most other methods DB agnostic:

public function __construct($id, $name, $address){
// ...
}

.... and then build a set of static methods to perform database reads:

public static function getById(PDO $db, $id){
// ....
return new Person($row['id'], $row['name'], $row['adress']);
}


> - should I pass the database handler as a parameter or should I do it in
> another way?

When I've done it that way I've found it quite practical.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: OOP, classes and databases [message #169444 is a reply to message #169436] Mon, 13 September 2010 10:49 Go to previous messageGo to next message
Piyush Gupta is currently offline  Piyush Gupta
Messages: 6
Registered: September 2010
Karma: 0
Junior Member
On Sep 13, 11:41 am, "Álvaro G. Vicario"
<alvaro.NOSPAMTH...@demogracia.com.invalid> wrote:
> El 10/09/2010 15:18, Mattias Campe escribió/wrote:
>
>> public function __construct ($id,$db) {
>> $query = ... the select query ...
>> $row = $db->query($query);
>> foreach ($db->query($query) as $row) {
>> $this->name = $row['name'];
>> $this->address = $row['address'];
>> }
>> }
> [...]
>> But I have the feeling that this isn't a 'best practice':
>> - I can't use this class without database
>> - should I put the select query and getting the data in a seperate
>> function, outside the constructor?
>
> You are right about this gotcha. One approach is to make your
> constructor and most other methods DB agnostic:
>
> public function __construct($id, $name, $address){
>         // ...
>
> }
>
> ... and then build a set of static methods to perform database reads:
>
> public static function getById(PDO $db, $id){
>         // ....
>         return new Person($row['id'], $row['name'], $row['adress']);
>
> }
>> - should I pass the database handler as a parameter or should I do it in
>> another way?
>
> When I've done it that way I've found it quite practical.
>
> --
> --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain
> -- Mi sitio sobre programación web:http://borrame.com
> -- Mi web de humor satinado:http://www.demogracia.com
> --

Hi ,

We at www.rntlabs.com [RNTLABS Software Solutions] have exciting
openings for the below

1/ PHP
2/ Designers
3/ Android
4/ iPhone
5/ QA
6/ ROR

Please apply at hr(at)rntlabs(dot)com ASAP .

Thanks

Piyush Gupta
Re: OOP, classes and databases [message #169472 is a reply to message #169414] Tue, 14 September 2010 18:52 Go to previous messageGo to next message
Mattias Campe is currently offline  Mattias Campe
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Op 10-09-10 21:48, Thomas Mlynarczyk schreef:
> Mattias Campe schrieb:
>
>> - PDO is better than mysql_query
> Yes. And use prepared statements.

Okay, I'll keep that in mind. I've searched for some more information
and one of the advantages seems to be about SQL injection.

>> - __construct is the way of making constructors
> Yes.

Okay.

>
>> - __get and __set is the way for getting and setting properties
> No. Usually properties are private and you write getFoo() and setFoo(
> $value ) methods to retrieve and set their values in a controlled way
> (allowing to validate the value to be set, for example). There may be
> circumstances where it is okay to allow anyone unrestricted access to a
> property. In this case, you can make the property public and don't need
> __get/__set. If you want to control access to a property, but still
> allow the simpler syntax $object->foo = 42, then you can use
> __get/__set. Also, if you want to allow public read-only access to a
> private property, you can use __get to achieve that.

Okay, makes sense.

>
>> class Person {
>> private $name;
>> private $address;
>>
>> public function __construct ($id,$db) {
>> $query = ... the select query ...
>> $row = $db->query($query);
>
> That last line above is not needed as you re-do the same query in the
> foreach loop below.
>
>> foreach ($db->query($query) as $row) {
>> $this->name = $row['name'];
>> $this->address = $row['address'];
>
> And anyway, that query should return at most one result (it is about one
> specific person, isn't it?), so there's no need for the loop. You could
> just do:

It's indeed about one specific person.

<snip code />
>
> Note: This code is untested and may even contain syntax errors or other
> typos. It is just meant for illustration.

There was only one closing bracket missing in :-):

$stmt->execute( array( $id );

>
>> public function __get ($var) {
>> switch ($var) {
>> case 'Name':
>
> You mean lowercase 'name' here.

Indeed.


<snip simplified getters /> Thanks, works perfectly!

>
>> But I have the feeling that this isn't a 'best practice':
>> - I can't use this class without database
>
> You could pass it an object which has the same methods as the PDO
> object, but retrieves data in a completely different way (like reading
> it from a text file or from an array). What you did is called dependency
> injection and is actually indeed a 'best practise'.

Good news :-).

> On the other hand --
> since your Person class seems to be just a simple data container, you
> might as well do it the other way round and tell PDO to give you back
> the data in form of a Person object:
>
> $stmt->fetchObject( 'Person' );
>
> But then you would need to make the properties public, so PDO can access
> them to put the values there (and remove all arguments from the
> constructor as they would not be needed). In this scenario, you would
> not pass the PDO object to the Person constructor, but pass the Person
> class to the PDO statement instead.


So, in this scenario, there wouldn't be any SQL inside the class Person,
so that I have to put the SQL-code in the "main" "class"? In that case,
I would prefer the other method, so that I can avoid SQL-code inside my
"main" "class".

>> - should I put the select query and getting the data in a seperate
>> function, outside the constructor?
>
> You could do it the way I suggested above. In any case, the name and
> address properties must be set when the Person object is created. You
> must not allow a Person object to exist without having its name and
> address set!

Thanks a lot Thomas, you clarified a lot of things for me!!! I think
that getting the basics right is important, so that's why I wanted some
professional advice, before I start to make more classes.

Greetings
Mattias
Re: OOP, classes and databases [message #169473 is a reply to message #169436] Tue, 14 September 2010 18:56 Go to previous messageGo to next message
Mattias Campe is currently offline  Mattias Campe
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Op 13-09-10 08:41, "Álvaro G. Vicario" schreef:
> El 10/09/2010 15:18, Mattias Campe escribió/wrote:
>> public function __construct ($id,$db) {
>> $query = ... the select query ...
>> $row = $db->query($query);
>> foreach ($db->query($query) as $row) {
>> $this->name = $row['name'];
>> $this->address = $row['address'];
>> }
>> }
> [...]
>> But I have the feeling that this isn't a 'best practice':
>> - I can't use this class without database
>> - should I put the select query and getting the data in a seperate
>> function, outside the constructor?
>
> You are right about this gotcha. One approach is to make your
> constructor and most other methods DB agnostic:
>
> public function __construct($id, $name, $address){
> // ...
> }
>
> ... and then build a set of static methods to perform database reads:
>
> public static function getById(PDO $db, $id){
> // ....
> return new Person($row['id'], $row['name'], $row['adress']);
> }

How could I use this class? Because I would need to make a Person from
the database, but first I would need to make a "random" Person, like:

$oPerson = new Person("000","something that will be overwritten","blabla");

$oPerson->getById($dbh,"245");


But I think that I'm not getting the picture right....

Greetings
Mattias
Re: OOP, classes and databases [message #169474 is a reply to message #169473] Tue, 14 September 2010 19:22 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
Mattias Campe schrieb:
>> public static function getById(PDO $db, $id){
>> // ....
>> return new Person($row['id'], $row['name'], $row['adress']);
>> }
>
> How could I use this class? Because I would need to make a Person from
> the database, but first I would need to make a "random" Person, like:
>
> $oPerson = new Person("000","something that will be overwritten","blabla");
>
> $oPerson->getById($dbh,"245");

No. The crucial thing here is the "static" keyword: "public STATIC
function getById(...)" That means you don't need a Person instance to
access it, you simply write:

$oPerson = Person::getById( $dbh, '245' );

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: OOP, classes and databases [message #169542 is a reply to message #169474] Fri, 17 September 2010 12:57 Go to previous messageGo to next message
Mattias Campe is currently offline  Mattias Campe
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Op 14-09-10 21:22, Thomas Mlynarczyk schreef:
> Mattias Campe schrieb:
>>> public static function getById(PDO $db, $id){
>>> // ....
>>> return new Person($row['id'], $row['name'], $row['adress']);
>>> }
>>
>> How could I use this class? Because I would need to make a Person from
>> the database, but first I would need to make a "random" Person, like:
>>
>> $oPerson = new Person("000","something that will be
>> overwritten","blabla");
>>
>> $oPerson->getById($dbh,"245");
>
> No. The crucial thing here is the "static" keyword: "public STATIC
> function getById(...)" That means you don't need a Person instance to
> access it, you simply write:
>
> $oPerson = Person::getById( $dbh, '245' );
>

Owkay, now I understand. So I could use that class in two ways:

Make a Person:
$oPerson1 = new Person("010","Johan","some adress");

Get an existing Person
$oPerson2 = Person::getById($dbh, '245');



So I think I'm getting it. Or at least almost: would it have the same
effect as using 2 constructors?

public function __construct($id, $name, $address){
// ...
}
public function __construct($id, $dbh){
// ...
}

$oPerson1 = new Person("010","Johan","some adress");
$oPerson2 = new Person($dbh, '245');


Greetings
Mattias
Re: OOP, classes and databases [message #169543 is a reply to message #169542] Fri, 17 September 2010 12:59 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 17/09/2010 14:57, Mattias Campe escribió/wrote:
> Op 14-09-10 21:22, Thomas Mlynarczyk schreef:
>> Mattias Campe schrieb:
>>>> public static function getById(PDO $db, $id){
>>>> // ....
>>>> return new Person($row['id'], $row['name'], $row['adress']);
>>>> }
>>>
>>> How could I use this class? Because I would need to make a Person from
>>> the database, but first I would need to make a "random" Person, like:
>>>
>>> $oPerson = new Person("000","something that will be
>>> overwritten","blabla");
>>>
>>> $oPerson->getById($dbh,"245");
>>
>> No. The crucial thing here is the "static" keyword: "public STATIC
>> function getById(...)" That means you don't need a Person instance to
>> access it, you simply write:
>>
>> $oPerson = Person::getById( $dbh, '245' );
>>
>
> Owkay, now I understand. So I could use that class in two ways:
>
> Make a Person:
> $oPerson1 = new Person("010","Johan","some adress");
>
> Get an existing Person
> $oPerson2 = Person::getById($dbh, '245');
>
>
>
> So I think I'm getting it. Or at least almost: would it have the same
> effect as using 2 constructors?
>
> public function __construct($id, $name, $address){
> // ...
> }
> public function __construct($id, $dbh){
> // ...
> }
>
> $oPerson1 = new Person("010","Johan","some adress");
> $oPerson2 = new Person($dbh, '245');

I'm not sure about what you mean exactly but in PHP you cannot have more
than one constructor. What I suggested is just a way to kind of separate
DB logic from application logic.




--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: OOP, classes and databases [message #169546 is a reply to message #169543] Fri, 17 September 2010 13:22 Go to previous messageGo to next message
Mattias Campe is currently offline  Mattias Campe
Messages: 7
Registered: September 2010
Karma: 0
Junior Member
Op 17-09-10 14:59, "Álvaro G. Vicario" schreef:
> El 17/09/2010 14:57, Mattias Campe escribió/wrote:
>> Op 14-09-10 21:22, Thomas Mlynarczyk schreef:
>>> Mattias Campe schrieb:
>>>> > public static function getById(PDO $db, $id){
>>>> > // ....
>>>> > return new Person($row['id'], $row['name'], $row['adress']);
>>>> > }
>>>>
>>>> How could I use this class? Because I would need to make a Person from
>>>> the database, but first I would need to make a "random" Person, like:
>>>>
>>>> $oPerson = new Person("000","something that will be
>>>> overwritten","blabla");
>>>>
>>>> $oPerson->getById($dbh,"245");
>>>
>>> No. The crucial thing here is the "static" keyword: "public STATIC
>>> function getById(...)" That means you don't need a Person instance to
>>> access it, you simply write:
>>>
>>> $oPerson = Person::getById( $dbh, '245' );
>>>
>>
>> Owkay, now I understand. So I could use that class in two ways:
>>
>> Make a Person:
>> $oPerson1 = new Person("010","Johan","some adress");
>>
>> Get an existing Person
>> $oPerson2 = Person::getById($dbh, '245');
>>
>>
>>
>> So I think I'm getting it. Or at least almost: would it have the same
>> effect as using 2 constructors?
>>
>> public function __construct($id, $name, $address){
>> // ...
>> }
>> public function __construct($id, $dbh){
>> // ...
>> }
>>
>> $oPerson1 = new Person("010","Johan","some adress");
>> $oPerson2 = new Person($dbh, '245');
>
> I'm not sure about what you mean exactly but in PHP you cannot have more
> than one constructor. What I suggested is just a way to kind of separate
> DB logic from application logic.
>

I was wondering if "my" way and your way had the same effect, but "my"
way seems to be impossible in PHP. In my search for more information I
stumbled across this site [1] which refers to the static way of working,
just like you taught me.

[1] http://alfonsojimenez.com/php/multiple-constructors-in-php/



Thank you for the help!
Mattias
Re: OOP, classes and databases [message #169549 is a reply to message #169546] Fri, 17 September 2010 15:16 Go to previous message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 17, 9:22 am, Mattias Campe <mattiaspuntca...@geeeemeil.com>
wrote:
> Op 17-09-10 14:59, "Álvaro G. Vicario" schreef:
>
>
>
>> El 17/09/2010 14:57, Mattias Campe escribió/wrote:
>>> Op 14-09-10 21:22, Thomas Mlynarczyk schreef:
>>>> Mattias Campe schrieb:
>>>> >> public static function getById(PDO $db, $id){
>>>> >> // ....
>>>> >> return new Person($row['id'], $row['name'], $row['adress']);
>>>> >> }
>
>>>> > How could I use this class? Because I would need to make a Person from
>>>> > the database, but first I would need to make a "random" Person, like:
>
>>>> > $oPerson = new Person("000","something that will be
>>>> > overwritten","blabla");
>
>>>> > $oPerson->getById($dbh,"245");
>
>>>> No. The crucial thing here is the "static" keyword: "public STATIC
>>>> function getById(...)" That means you don't need a Person instance to
>>>> access it, you simply write:
>
>>>> $oPerson = Person::getById( $dbh, '245' );
>
>>> Owkay, now I understand. So I could use that class in two ways:
>
>>> Make a Person:
>>> $oPerson1 = new Person("010","Johan","some adress");
>
>>> Get an existing Person
>>> $oPerson2 = Person::getById($dbh, '245');
>
>>> So I think I'm getting it. Or at least almost: would it have the same
>>> effect as using 2 constructors?
>
>>> public function __construct($id, $name, $address){
>>> // ...
>>> }
>>> public function __construct($id, $dbh){
>>> // ...
>>> }
>
>>> $oPerson1 = new Person("010","Johan","some adress");
>>> $oPerson2 = new Person($dbh, '245');
>
>> I'm not sure about what you mean exactly but in PHP you cannot have more
>> than one constructor. What I suggested is just a way to kind of separate
>> DB logic from application logic.
>
> I was wondering if "my" way and your way had the same effect, but "my"
> way seems to be impossible in PHP. In my search for more information I
> stumbled across this site [1] which refers to the static way of working,
> just like you taught me.

By "your" way, you are talking about method overloading, which is
sadly not supported in PHP. Instead, you have to use optional or
undefined arguments and parse through them in your constructor, which
I find to be very tedious.

class Person {
function __construct($reqField) {
// parse arguments
}

/*** OR ***/

function __construct($reqField, $optField = null) {
// test $optField
}
}

// this is valid syntax, even if the function
// definition doesn't declare both arguments
$p = new Person($myRequiredField, $myOptionalField)

This approach works pretty well if all constructor calling schemes
have a different number of arguments. Otherwise, I would use the
method shown in the link you provided.

> [1]http://alfonsojimenez.com/php/multiple-constructors-in-php/

For my DAO objects, I tend to use the following methods:

function __construct ($dbHandle, $primaryKey) {
// load data and set class members
}

static function create ($dbHandle, $dataArray) {
// write db
// return new instance of class
}

//so, to instantiate an existing person,
$p = new Person($DB, 1);

// to create and instantiate a new person
$data = array("first" => "Tom", "last" => "Foolery");
$p = Person::create($DB, $data);

If I need to instantiate an existing Person by first name, last name,
I usually will have this:

function __construct($DB, $arg1, $arg2 = null)
{
if ($arg2 === null)
// query db by primary key
else
// query db by firstname,lastname

// set class members
}

However, if you needed to instantiate an object two different ways and
both ways accept two arguments that are both strings, then method
overloading wouldn't help you anyway, even in a language that supports
it. In that case, there's little choice but to write your own
separate functions that are basically constructors, but aren't called
"__construct()" and are called statically.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Best PHP way to connect to a DB across multiple pages ?
Next Topic: When do I use {}?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Sep 20 08:27:11 GMT 2024

Total time taken to generate the page: 0.02050 seconds