Re: [PHP] calling parent class method from the outside
admin wrote: Jochem Maas wrote: ... the 'callback' type has a number of forms: 'myFunc' array('className', 'myMeth') array(self, 'myMeth') array(parent, 'myMeth') array($object, 'myMeth') self and parent adhere to the same 'context' rules when used in call_user_func*() as when you use them directly - whether $this is present within the scope of the called method is essentially down to whether the method being called is defined as static or not. AFAIK call_user_func*() respects PPP modifiers and works transparently with regard to access to the relevant object variable ($this) This undocumented (?) feature is fairly nice. it is documented - where do you think I find out about it? Unfortunately there's no $this outside the class. this seems painfully obvious - not sure why this is a problem it's not like $this is javascript. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calling parent class method from the outside
Jochem Maas wrote: admin wrote: Jochem Maas wrote: ... the 'callback' type has a number of forms: 'myFunc' array('className', 'myMeth') array(self, 'myMeth') array(parent, 'myMeth') array($object, 'myMeth') self and parent adhere to the same 'context' rules when used in call_user_func*() as when you use them directly - whether $this is present within the scope of the called method is essentially down to whether the method being called is defined as static or not. AFAIK call_user_func*() respects PPP modifiers and works transparently with regard to access to the relevant object variable ($this) This undocumented (?) feature is fairly nice. it is documented - where do you think I find out about it? I knew it could be, and it is described at the top: http://www.php.net/manual/en/language.oop5.basic.php Thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] calling parent class method from the outside
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()) ...; Some contrived example to illustrate the point: class AParent { public function foo() { .. } } class A extends AParent { public function foo() { doit($this, __CLASS__, __FUNCTION__); } } function doit($obj, $classname, $funcname) { if (...) //$obj-classname_parent::$funcname(); } Thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calling parent class method from the outside
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()) ...; there is nothing in the language that provides this functionality and I would argue that the caller should be able to do such things ... the object should be in control of the functionality it provides not the user. hmm, I don't think I explained that very well - I'm having a bit of a brain freeze ... if you need to expose parent::foo() via a subclass then you shouldn't override it in the subclass but rather create a foo2() (for instance) Some contrived example to illustrate the point: class AParent { public function foo() { .. } } class A extends AParent { public function foo() { doit($this, __CLASS__, __FUNCTION__); } } function doit($obj, $classname, $funcname) { if (...) //$obj-classname_parent::$funcname(); } Thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calling parent class method from the outside
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()) ...; Not sure I get your requirement exactly, but you could try this: class A { public function foo () { // ... } } class B { public parent; // Constructor public function __construct () { $this-parent = new A(); } } // And then... $b = new B(); $b-parent-foo(); -- Richard Heyes +44 (0)844 801 1072 http://www.websupportsolutions.co.uk Currently looking for partnerships -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calling parent class method from the outside
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()) ...; Some contrived example to illustrate the point: class AParent { public function foo() { .. } } class A extends AParent { public function foo() { doit($this, __CLASS__, __FUNCTION__); } } function doit($obj, $classname, $funcname) { if (...) //$obj-classname_parent::$funcname(); } Thanks. To use Richards example, but with a little different twist for accessing the parent methods/properties class A { public __construct() { // ... } public function foo () { // ... } } class B extends A { // Constructor public function __construct () { parent::__construct(); } } // And then... $b = new B(); $b-foo(); This will bring all methods/properties from class A into class B. You can choose to override class A methods/properties when you define class B. But the idea here is that only need to write the methods/properties once in the parent class, then you can extend your class with class A and have all the methods/properties available to you within your current class. It is good to practice the DRY principle here. Note: Richard Heyes thanks for the code snippet -- 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] calling parent class method from the outside
Jim Lucas 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()) ...; Some contrived example to illustrate the point: class AParent { public function foo() { .. } } class A extends AParent { public function foo() { doit($this, __CLASS__, __FUNCTION__); } } function doit($obj, $classname, $funcname) { if (...) //$obj-classname_parent::$funcname(); } Thanks. To use Richards example, but with a little different twist for accessing the parent methods/properties class A { public __construct() { // ... } public function foo () { // ... } } class B extends A { // Constructor public function __construct () { parent::__construct(); } } // And then... $b = new B(); $b-foo(); This will bring all methods/properties from class A into class B. both methods properties of the parent (class A) are available assuming they are public or protected - if you don't call the parent ctor the only thing your missing is whatever initialization of data and/or property values (etc) would have been done by the parent ctor. class A { public function foo() {echo achoo;} } class B extends A {} $b = new B; $b-foo(); it is quite normal to call parent::__construct(); in the subclass' ctor but it's not required to make methods/properties available and I don't see what baring it has on the OP's question. another solution for the OP might be (although I think it goes against all design principles): class A { function foo() { echo achoo\n; } } class B extends A { function foo() { echo cough\n; } function __call($meth, $args) { $func = array(parent, strtolower(str_replace(parent,, $meth))); if (is_callable($func)) return call_user_func_array($func, $args); } } $b = new B; $b-foo(); $b-parentFoo(); You can choose to override class A methods/properties when you define class B. But the idea here is that only need to write the methods/properties once in the parent class, then you can extend your class with class A and have all the methods/properties available to you within your current class. It is good to practice the DRY principle here. Note: Richard Heyes thanks for the code snippet -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calling parent class method from the outside
Jochem Maas wrote: another solution for the OP might be (although I think it goes against all design principles): class A { function foo() { echo achoo\n; } } class B extends A { function foo() { echo cough\n; } function __call($meth, $args) { $func = array(parent, strtolower(str_replace(parent,, $meth))); if (is_callable($func)) return call_user_func_array($func, $args); } } $b = new B; $b-foo(); $b-parentFoo(); Barring the s/parent/get_parent_class/ the idea is really really cute, thanks. Beautiful. Heck, maybe I'll even hack the code to do it like that... after some grace period. P.S.: I thought calling array('classname', 'method') only worked for static methods? It turns out there's an implied $this being passed around? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calling parent class method from the outside
admin wrote: Jochem Maas wrote: another solution for the OP might be (although I think it goes against all design principles): class A { function foo() { echo achoo\n; } } class B extends A { function foo() { echo cough\n; } function __call($meth, $args) { $func = array(parent, strtolower(str_replace(parent,, $meth))); if (is_callable($func)) return call_user_func_array($func, $args); } } $b = new B; $b-foo(); $b-parentFoo(); Barring the s/parent/get_parent_class/ the idea is really really cute, thanks. Beautiful. Heck, maybe I'll even hack the code to do it like that... after some grace period. sure it might be a neat little hack - __call() can be used for alsorts of wonderful madness but do note it can become a maintainance and/or documentation nightmare ... not to mention that it doesn't work with code completion in any editor I know. and having read what you wrote about Propel/Symphony I still don't get what the problem is that your trying to solve ... although I suspect that your probably suffering from a lack of late static binding (which, if we had it, could be used to minimize alot of repetitive boiler plate code in [for instance] 'data object' classes that extend a common base ... then again I may be wrong :-) [Do not get me started on LSB - I've been moaning about it since 5.0RC3] P.S.: I thought calling array('classname', 'method') only worked for static methods? It turns out there's an implied $this being passed around? the 'callback' type has a number of forms: 'myFunc' array('className', 'myMeth') array(self, 'myMeth') array(parent, 'myMeth') array($object, 'myMeth') self and parent adhere to the same 'context' rules when used in call_user_func*() as when you use them directly - whether $this is present within the scope of the called method is essentially down to whether the method being called is defined as static or not. AFAIK call_user_func*() respects PPP modifiers and works transparently with regard to access to the relevant object variable ($this) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calling parent class method from the outside
Jochem Maas wrote: admin wrote: Jochem Maas wrote: another solution for the OP might be (although I think it goes against all design principles): class A { function foo() { echo achoo\n; } } class B extends A { function foo() { echo cough\n; } function __call($meth, $args) { $func = array(parent, strtolower(str_replace(parent,, $meth))); if (is_callable($func)) return call_user_func_array($func, $args); } } $b = new B; $b-foo(); $b-parentFoo(); Barring the s/parent/get_parent_class/ the idea is really really cute, thanks. Beautiful. Heck, maybe I'll even hack the code to do it like that... after some grace period. sure it might be a neat little hack - __call() can be used for alsorts of wonderful madness It was late in the night over where I live when I replied :-) But now I see that although your trick answers the subj perfectly, back then I didn't make it clear that the real intention was to get rid of code duplication in doSetColumn(). On closer inspection your trick would force me to scatter identical copies of __call() in every model class too, so I've gained nothing. [snip] and having read what you wrote about Propel/Symphony I still don't get what the problem is that your trying to solve ... although I suspect In Symfony/Propel you're on your own when things get to the point where you have to put the submitted data into DB. The main idea is that for columns that can be NULL the form submits empty values as empty string '', but I'd rather make that NULL. You would argue that simply leaving out the relevant update from the SQL clause would do the trick, but a second, more important issue prevents skipping NULL and is the basis for the former: for everything to work smoothly and transparently Symfony/Propel _insists_ on pre-hydrating the object right before the update, a requirement I cannot personally stand. So I've tried working around the requirement by populating the model myself with form data using setNew(false) and setXXX() calls. The real doSetColumn(): # these mutators help to skip pre-hydrating the object just to do an update # treats NULL and empty string the same # this also means empty input string will be converted to SQL NULL private function doSetColumn($colname, $value, $col_mutator, $mod_trigger_value = '') { if ($value == '') $value = null; if (call_user_func(array(__CLASS__ . 'Peer', 'getTableMap'))-getColumn($colname)-isNotNull()) { if (is_null($value)) throw new Exception($col_mutator: $colname may not be NULL); } else { if (is_null($value) !$this-isColumnModified($colname)) # XXX: as we don't know the current value in DB # we need to force it to be set to NULL parent::$col_mutator($mod_trigger_value); } parent::$col_mutator($value); } Hope you get the idea. P.S.: I thought calling array('classname', 'method') only worked for static methods? It turns out there's an implied $this being passed around? the 'callback' type has a number of forms: 'myFunc' array('className', 'myMeth') array(self, 'myMeth') array(parent, 'myMeth') array($object, 'myMeth') self and parent adhere to the same 'context' rules when used in call_user_func*() as when you use them directly - whether $this is present within the scope of the called method is essentially down to whether the method being called is defined as static or not. AFAIK call_user_func*() respects PPP modifiers and works transparently with regard to access to the relevant object variable ($this) This undocumented (?) feature is fairly nice. Unfortunately there's no $this outside the class. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php