Re: [PHP] Extending a class with a static constructur (like PEAR::DB)

2006-03-10 Thread Andreas Korthaus

[EMAIL PROTECTED] wrote:


Ahhh! I'd kept thinking what connect() returned was a db object, but it does 
look like
it returns varying objects depending on which database you're using.


correct



Maybe I'd want to extend DB_common instead of DB_mysql, so that the methods 
would
be inhereted by any object?


But DB_mysql will not inherit from your object! Look at the source:

class DB_mysql extends DB_common {...}

If you inherit from a class, the parent class will not inherit your (the 
child classes) methods. If a class should inherit from your object, it 
must explicitly inherit from that class.



Best regards
Andreas

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Extending a class with a static constructur (like PEAR::DB)

2006-03-10 Thread Andreas Korthaus

Chris wrote:


You'll need to:

$dbtoy = new DBToyExt();
$dbtoy-connect()
$dbtoy-testext('testing');


But you cannot do:

$dbtoy = new DBToyExt();
$dbtoy-connect()
$dbtoy-testext('testing');
$result = $dbtoy-query('SELECT...');
//[...]


You have to do something like this:

$dbtoy = new DBToyExt();
$db = $dbtoy-connect()
$dbtoy-testext('testing');
$result = $db-query('SELECT...');
//[...]

If you want to avoid that, you have to write a wrapper around DB.


best regards
Andreas

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Extending a class with a static constructur (like PEAR::DB)

2006-03-09 Thread Chris

[EMAIL PROTECTED] wrote:
So... I'm trying to extend PEAR::DB. It'd be great to keep everything it offers and just add a few more perhaps unconventional functions. 


Intuitively, it seemed like this approach might work:

?PHP

require_once(DB.php);

# Toy Extension of DB Class
class DBToyExt extends DB
{
var $foo = 1;
var $bar = 2;

function testext($x)
{
echo \nHEY: $x;
}
}

$dte = DBToyExt::connect(mysql://weston_tssa:[EMAIL 
PROTECTED]/weston_tssa);

$dte-testext('testing');
$dte-testext($dte-moo);
$dte-testext($dte-bar);

?  


However, it doesn't seem to understand that the method testext exists, and 
gives me a fatal error to that effect, as you can see:


$dte will only have the return value of DBToyExt::connect() - it won't 
allow you to access other methods in the class.


You'll need to:

$dbtoy = new DBToyExt();
$dbtoy-connect()
$dbtoy-testext('testing');

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Extending a class with a static constructur (like PEAR::DB)

2006-03-09 Thread weston
Weston wrote:

   $dte = DBToyExt::connect(mysql://weston_tssa:[EMAIL 
  PROTECTED]/weston_tssa);
 
   $dte-testext('testing');
   $dte-testext($dte-moo);
   $dte-testext($dte-bar);

On Fri, Mar 10, 2006 at 10:43:02AM +1100, Chris wrote:

 $dte will only have the return value of DBToyExt::connect() - it won't
 allow you to access other methods in the class.

 You'll need to:

 $dbtoy = new DBToyExt();
 $dbtoy-connect()
 $dbtoy-testext('testing');

Thanks! Works like a charm:

http://weston.canncentral.org/web_lab/mlib/DBToyExt2.php

That's interesting. I think I just sortof expected that since the canonical
invocation is through a statically called method, calling it by dereferencing
a specific object wouldn't work. 

Does anyone know if that would also work in PHP 5?

If not, is there another way to do what I'm trying to do?

Thanks,

Weston

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Extending a class with a static constructur (like PEAR::DB)

2006-03-09 Thread Chris

[EMAIL PROTECTED] wrote:

Weston wrote:



$dte = DBToyExt::connect(mysql://weston_tssa:[EMAIL PROTECTED]/weston_tssa);

$dte-testext('testing');
$dte-testext($dte-moo);
$dte-testext($dte-bar);



$dte will only have the return value of DBToyExt::connect() - it won't
allow you to access other methods in the class.

You'll need to:

$dbtoy = new DBToyExt();
$dbtoy-connect()
$dbtoy-testext('testing');



Thanks! Works like a charm:

http://weston.canncentral.org/web_lab/mlib/DBToyExt2.php

That's interesting. I think I just sortof expected that since the canonical
invocation is through a statically called method, calling it by dereferencing
a specific object wouldn't work. 


In your example, $dte holds whatever DBToyExt::connect returns (whether 
that's a connection resource, a boolean, a string - doesn't matter).


It's the same as:

function connect() {
  return true;
}

$x = connect();

$x will only hold what 'connect' returns.


If you do:

var_dump($dte);

It won't be an object, so you can't use that to reference other methods 
in that (or the parent) class(es).


--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Extending a class with a static constructur (like PEAR::DB)

2006-03-09 Thread weston
  So... I'm trying to extend PEAR::DB. It'd be great to keep everything it 
  offers and just add a few more perhaps unconventional functions. 
  
  $dte = DBToyExt::connect(mysql://weston_tssa:[EMAIL 
  PROTECTED]/weston_tssa);
  
 
 DB::connect() is actually a factory call. So what is returned is an
 instance of (in your case) a DB_mysql object. 

Ahhh! I'd kept thinking what connect() returned was a db object, but it does 
look like
it returns varying objects depending on which database you're using.

Maybe I'd want to extend DB_common instead of DB_mysql, so that the methods 
would
be inhereted by any object?

Thanks,

Weston

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php