Re: [PHP] Re: calling parent class method from the outside

2007-07-02 Thread Richard Lynch
On Mon, July 2, 2007 3:44 pm, admin wrote:

IMHO, you were f'ed from the microsecond where you decided it was a
Good Idea (tm) to have an object instance for each row in the DB...

That just plain won't scale up very well at all for a large table, if
you ever need to get code re-use and do something to every row in PHP.

PHP is not C++

If you want to write C++, go ahead and write C++

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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



Re: [PHP] Re: calling parent class method from the outside

2007-07-02 Thread admin

Jim Lucas wrote:

Rihad wrote:


Now will you mentally copy and paste the above code several times, 
doing the necessary text substitutions, what will you get? Three 
identical copies of doSetColumn() in each class! And the real 
doSetColumn() is a bit heavier than a one-liner. We come full circle.




I don't understand, which part(s) are you copying multiple times?  Foo, 
FooBase, all the above...


if you are talking about having multiple Foo classes with one common 
FooBase class for each that they all inherit then it would be simple, 
move soSetColumn() to the FooBase class.



[snipped]

In short: there are several sets of Xxx, XxxPeer, BaseXxx, BaseXxxPeer.

The long and uncut story: please take a look at how Symfony and more 
specifically its ORM Propel does things to better understand the 
nightmare I'm talking about. Computers don't have a hard time with code 
duplication when generating source code: they can always scratch things 
and start anew in a second (or half a second on a dual-cpu box). Code 
generation is the latest hot topic that lays the "Web 2.0" buzzword 
hands down. The funny part starts when humans, who are not really good 
at code duplication, try interfacing with the kilobytes of generated 
masses of source code at the API level. You find yourself repeating code 
everywhere, and it occurs so naturally that you begin to kind of like it 
that way. Just kidding.


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



Re: [PHP] Re: calling parent class method from the outside

2007-07-02 Thread Jim Lucas

Rihad wrote:


Now will you mentally copy and paste the above code several times, doing 
the necessary text substitutions, what will you get? Three identical 
copies of doSetColumn() in each class! And the real doSetColumn() is a 
bit heavier than a one-liner. We come full circle.




I don't understand, which part(s) are you copying multiple times?  Foo, 
FooBase, all the above...

if you are talking about having multiple Foo classes with one common FooBase class for each that 
they all inherit then it would be simple, move soSetColumn() to the FooBase class.


Maybe this?

doSetColumn(FooPeer::BAR(), $value, __FUNCTION__);
}
function setBaz($value) {
$this->doSetColumn(FooPeer::BAZ(), $value, __FUNCTION__);
}
function setXyzzy($value) {
$this->doSetColumn(FooPeer::XYZZY(), $value, __FUNCTION__);
}
}
class Foo2 extends BaseFoo {
function setBar($value) {
$this->doSetColumn(FooPeer::BAR(), $value, __FUNCTION__);
}
function setBaz($value) {
$this->doSetColumn(FooPeer::BAZ(), $value, __FUNCTION__);
}
function setXyzzy($value) {
$this->doSetColumn(FooPeer::XYZZY(), $value, __FUNCTION__);
}
}

echo "";

$Foo1 = new Foo1();
$Foo1->setBar('my value for bar');
$Foo1->setBaz('my value for baz');
$Foo1->setXyzzy('my value for xyzzy');

$Foo2 = new Foo2();
$Foo2->setBar('my value for bar');
$Foo2->setBaz('my value for baz');
$Foo2->setXyzzy('my value for xyzzy');


--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

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



Re: [PHP] Re: calling parent class method from the outside

2007-07-02 Thread Rihad

Jim Lucas wrote:

admin wrote:

OK, here we go: Propel in Symfony uses generated model classes
representing DB rows, stub classes inheriting from them ready to be used
(such as below), and accessors representing data columns:


[snipped]
This is a little different then what you are trying above, but maybe it 
is close to what you are looking for:


doSetColumn(FooPeer::BAR(), $value, __FUNCTION__);
}

function setBaz($value) {
$this->doSetColumn(FooPeer::BAZ(), $value, __FUNCTION__);
}

function setXyzzy($value) {
$this->doSetColumn(FooPeer::XYZZY(), $value, __FUNCTION__);
}

function doSetColumn($colname, $value, $col_mutator) {
parent::$col_mutator($value);
}
}

$Foo = new Foo();
echo "";
$Foo->setBar('my value for bar');
$Foo->setBaz('my value for baz');
$Foo->setXyzzy('my value for xyzzy');



Now will you mentally copy and paste the above code several times, doing 
the necessary text substitutions, what will you get? Three identical 
copies of doSetColumn() in each class! And the real doSetColumn() is a 
bit heavier than a one-liner. We come full circle.


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



Re: [PHP] Re: calling parent class method from the outside

2007-07-02 Thread Jim Lucas

admin wrote:

Colin Guthrie wrote:

admin wrote:

Inside the body of method foo() you can of course use syntax like
parent::foo(). But is there a way to call the parent version of
obj->foo() outside the class? That kind of syntax is allowed in C++, for
example: Aclass a; if (a.Aparent::foo()) ...;


[snipped]


I agree with Jochem that if you find you need to do this, then you're
probably not designing the code correctly.



OK, here we go: Propel in Symfony uses generated model classes
representing DB rows, stub classes inheriting from them ready to be used
(such as below), and accessors representing data columns:

class Foo extends BaseFoo
{
public function setBar($value)
{
$this->doSetColumn(FooPeer::BAR, $value, __FUNCTION__);
}

public function setBaz($value)
{
$this->doSetColumn(FooPeer::BAZ, $value, __FUNCTION__);
}

public function setXyzzy($value)
{
$this->doSetColumn(FooPeer::XYZZY, $value, __FUNCTION__);
}

// several more of these, and finally...

private function doSetColumn($colname, $value, $col_mutator)
{
/* setter does what it has to do and calls the parent
 * version of self to do the real work of modifying
 * state
 */
// ...
if ($something)

What are you wanting to test for?


parent::$col_mutator($junk_to_trigger_modified);

does this setup the next call to $col_mutator() ?  when you pass the value?


// ...

parent::$col_mutator($value);
}
}

There are several models (tables) such as Foo, and they all have the
same body of doSetColumn(), so a logical next step was to factor that
method away _and_ leave most of the code intact, in which I've failed.

Once again, calling the parent version of a method "externally" is
allowed in C++ (and whoever said it was bad design should speak up to
Bjarne Stroustrup ;-)) Any such trick in PHP?



This is a little different then what you are trying above, but maybe it is close to what you are 
looking for:


doSetColumn(FooPeer::BAR(), $value, __FUNCTION__);
}

function setBaz($value) {
$this->doSetColumn(FooPeer::BAZ(), $value, __FUNCTION__);
}

function setXyzzy($value) {
$this->doSetColumn(FooPeer::XYZZY(), $value, __FUNCTION__);
}

function doSetColumn($colname, $value, $col_mutator) {
parent::$col_mutator($value);
}
}

$Foo = new Foo();
echo "";
$Foo->setBar('my value for bar');
$Foo->setBaz('my value for baz');
$Foo->setXyzzy('my value for xyzzy');





--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

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