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
Return to the default flat view Create a new topic Submit Reply
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:
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.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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: Sun Nov 24 19:59:09 GMT 2024

Total time taken to generate the page: 0.04860 seconds