how to overload accessible methods
how to overload accessible methods
am 13.04.2010 17:39:46 von Ryan Sun
As we all know, __call() can overload non-accessible methods,
eg.
Class User
{
public function __call($name, $args)
{
//validate user....
$this->_validate();
$this->_{$name}($args);
}
private function _validate()
{
//....
}
private function _update($args)
{
//....
}
}
$user = new User();
$user->update() // will call _validate before _update automatically
BUT, if I want to make this update a public function, how can I call
the validate without call it inside update function explicitly?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: how to overload accessible methods
am 13.04.2010 17:46:37 von Nathan Rixham
Ryan Sun wrote:
> As we all know, __call() can overload non-accessible methods,
> eg.
> Class User
> {
> public function __call($name, $args)
> {
> //validate user....
> $this->_validate();
>
> $this->_{$name}($args);
> }
> private function _validate()
> {
> //....
> }
> private function _update($args)
> {
> //....
> }
> }
>
> $user = new User();
> $user->update() // will call _validate before _update automatically
>
> BUT, if I want to make this update a public function, how can I call
> the validate without call it inside update function explicitly?
why would you want to, is there a technical reason for wanting magic
functionality instead of "normal" functionality (+ wouldn't it make the
code much easier to maintain and debug if developers can see what is
called where, instead of just magic).
to answer though, you're best bet is probably to make an abstract class
with magic functionality and then extend with an implementing public class.
abstract class MagicUser
{
public function __call($name, $args)
{
//validate user....
$this->_validate();
$this->_{$name}($args);
}
private function _validate()
{
//....
}
private function _update($args)
{
//....
}
}
class User extends MagicUser
{
public function update($args)
{
parent::update($args);
}
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: how to overload accessible methods
am 13.04.2010 18:01:00 von Ashley Sheridan
--=-2F0W9YbhQMog3jP7Ujct
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Tue, 2010-04-13 at 12:03 -0400, Ryan Sun wrote:
> I'm writing an abstract parent class which only contain a validate
> method, other developers will extend this class and add many new
> public methods, every new methods will need to perform a validate
> first. Won't it be good if validate get called automatically before
> every method call so that they don't have to write more code and they
> won't miss this validate?
>
> On Tue, Apr 13, 2010 at 11:46 AM, Nathan Rixham wrote:
> > Ryan Sun wrote:
> >> As we all know, __call() can overload non-accessible methods,
> >> eg.
> >> Class User
> >> {
> >> public function __call($name, $args)
> >> {
> >> //validate user....
> >> $this->_validate();
> >>
> >> $this->_{$name}($args);
> >> }
> >> private function _validate()
> >> {
> >> //....
> >> }
> >> private function _update($args)
> >> {
> >> //....
> >> }
> >> }
> >>
> >> $user = new User();
> >> $user->update() // will call _validate before _update automatically
> >>
> >> BUT, if I want to make this update a public function, how can I call
> >> the validate without call it inside update function explicitly?
> >
> >
> > why would you want to, is there a technical reason for wanting magic
> > functionality instead of "normal" functionality (+ wouldn't it make the
> > code much easier to maintain and debug if developers can see what is
> > called where, instead of just magic).
> >
> > to answer though, you're best bet is probably to make an abstract class
> > with magic functionality and then extend with an implementing public class.
> >
> > abstract class MagicUser
> > {
> > public function __call($name, $args)
> > {
> > //validate user....
> > $this->_validate();
> > $this->_{$name}($args);
> > }
> >
> > private function _validate()
> > {
> > //....
> > }
> > private function _update($args)
> > {
> > //....
> > }
> > }
> >
> > class User extends MagicUser
> > {
> > public function update($args)
> > {
> > parent::update($args);
> > }
> > }
> >
> >
>
That would mean that the class can only be extended as far as your
validation code allows, and the developers extending your class will
have little control over how anything is validated.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-2F0W9YbhQMog3jP7Ujct--
Re: how to overload accessible methods
am 13.04.2010 18:03:55 von Ryan Sun
I'm writing an abstract parent class which only contain a validate
method, other developers will extend this class and add many new
public methods, every new methods will need to perform a validate
first. Won't it be good if validate get called automatically before
every method call so that they don't have to write more code and they
won't miss this validate?
On Tue, Apr 13, 2010 at 11:46 AM, Nathan Rixham wrote:
> Ryan Sun wrote:
>> As we all know, __call() can overload non-accessible methods,
>> eg.
>> Class User
>> {
>> =A0 =A0 public function __call($name, $args)
>> =A0 =A0 {
>> =A0 =A0 =A0 =A0 //validate user....
>> =A0 =A0 =A0 =A0 $this->_validate();
>>
>> =A0 =A0 =A0 =A0 $this->_{$name}($args);
>> =A0 =A0 }
>> =A0 =A0 private function _validate()
>> =A0 =A0 {
>> =A0 =A0 =A0 =A0 //....
>> =A0 =A0 }
>> =A0 =A0 private function _update($args)
>> =A0 =A0 {
>> =A0 =A0 =A0 =A0 //....
>> =A0 =A0 }
>> }
>>
>> $user =3D new User();
>> $user->update() // will call _validate before _update automatically
>>
>> BUT, if I want to make this update a public function, how can I call
>> the validate without call it inside update function explicitly?
>
>
> why would you want to, is there a technical reason for wanting magic
> functionality instead of "normal" functionality (+ wouldn't it make the
> code much easier to maintain and debug if developers can see what is
> called where, instead of just magic).
>
> to answer though, you're best bet is probably to make an abstract class
> with magic functionality and then extend with an implementing public clas=
s.
>
> abstract class MagicUser
> {
> =A0 =A0public function __call($name, $args)
> =A0 =A0{
> =A0 =A0 =A0 =A0//validate user....
> =A0 =A0 =A0 =A0$this->_validate();
> =A0 =A0 =A0 =A0$this->_{$name}($args);
> =A0 =A0}
>
> =A0 =A0private function _validate()
> =A0 =A0{
> =A0 =A0 =A0 =A0//....
> =A0 =A0}
> =A0 =A0private function _update($args)
> =A0 =A0{
> =A0 =A0 =A0 =A0//....
> =A0 =A0}
> }
>
> class User extends MagicUser
> {
> =A0 =A0public function update($args)
> =A0 =A0{
> =A0 =A0 =A0 =A0parent::update($args);
> =A0 =A0}
> }
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: how to overload accessible methods
am 13.04.2010 18:11:29 von Nathan Rixham
Ryan Sun wrote:
> I'm writing an abstract parent class which only contain a validate
> method, other developers will extend this class and add many new
> public methods, every new methods will need to perform a validate
> first. Won't it be good if validate get called automatically before
> every method call so that they don't have to write more code and they
> won't miss this validate?
This may call for a back to roots approach, what exactly are you trying
to accomplish, as in: what is the validation doing?
perhaps if we see the full picture, we can recommend another perhaps
more suited approach to the full thing, feel free to post the full code
if you want / can, the more info the better!
Regards,
Nathan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: how to overload accessible methods
am 13.04.2010 18:25:58 von Ryan Sun
this is a class for corntab job, and the validation is very simple,
just check if the status of user is active when cron job runs, if not,
throws an exception, other developers won't want to overwrite this
validation.
which method of user class will be called is configurable via website
backed page(we write the name of methods directly in to schedule
table).
Using private methods will solve the problem but since we write public
methods for all the other cron classes, I just want to keep the style
to make less confusion.
On Tue, Apr 13, 2010 at 12:11 PM, Nathan Rixham wrote:
> Ryan Sun wrote:
>> I'm writing an abstract parent class which only contain a validate
>> method, other developers will extend this class and add many new
>> public methods, every new methods will need to perform a validate
>> first. =A0Won't it be good if validate get called automatically before
>> every method call so that they don't have to write more code and they
>> won't miss this validate?
>
> This may call for a back to roots approach, what exactly are you trying
> to accomplish, as in: what is the validation doing?
>
> perhaps if we see the full picture, we can recommend another perhaps
> more suited approach to the full thing, feel free to post the full code
> if you want / can, the more info the better!
>
> Regards,
>
> Nathan
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: how to overload accessible methods
am 15.04.2010 10:46:10 von Richard Quadling
On 13 April 2010 17:25, Ryan Sun wrote:
> this is a class for corntab job, and the validation is very simple,
> just check if the status of user is active when cron job runs, if not,
> throws an exception, other developers won't want to overwrite this
> validation.
> which method of user class will be called is configurable via website
> backed page(we write the name of methods directly in to  schedule
> table).
> Using private methods will solve the problem but since we write public
> methods for all the other cron classes, I just want to keep the style
> to make less confusion.
>
> On Tue, Apr 13, 2010 at 12:11 PM, Nathan Rixham wrote=
:
>> Ryan Sun wrote:
>>> I'm writing an abstract parent class which only contain a validate
>>> method, other developers will extend this class and add many new
>>> public methods, every new methods will need to perform a validate
>>> first. Â Won't it be good if validate get called automatically befo=
re
>>> every method call so that they don't have to write more code and they
>>> won't miss this validate?
>>
>> This may call for a back to roots approach, what exactly are you trying
>> to accomplish, as in: what is the validation doing?
>>
>> perhaps if we see the full picture, we can recommend another perhaps
>> more suited approach to the full thing, feel free to post the full code
>> if you want / can, the more info the better!
>>
>> Regards,
>>
>> Nathan
>>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Would this be better ...
abstract class baseValidate() {
final public function __construct($params) {
// Determine if user is active.
// If OK, then call abstract function postConstruct($params)
}
abstract function postConstruct($params);
}
You can't override the constructor, so the validation will always be
called. The developer's can implement their own postConstruct as if
they where extending __construct.
--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re: how to overload accessible methods
am 16.04.2010 16:46:49 von Ryan Sun
thanks for all your reply, since all these classes have main entry in
cron.php, I have moved validation there.
Richard, your solution seems interesting, maybe I can use it later :)
On Thu, Apr 15, 2010 at 4:46 AM, Richard Quadling
wrote:
> On 13 April 2010 17:25, Ryan Sun wrote:
>> this is a class for corntab job, and the validation is very simple,
>> just check if the status of user is active when cron job runs, if not,
>> throws an exception, other developers won't want to overwrite this
>> validation.
>> which method of user class will be called is configurable via website
>> backed page(we write the name of methods directly in to =A0schedule
>> table).
>> Using private methods will solve the problem but since we write public
>> methods for all the other cron classes, I just want to keep the style
>> to make less confusion.
>>
>> On Tue, Apr 13, 2010 at 12:11 PM, Nathan Rixham wrot=
e:
>>> Ryan Sun wrote:
>>>> I'm writing an abstract parent class which only contain a validate
>>>> method, other developers will extend this class and add many new
>>>> public methods, every new methods will need to perform a validate
>>>> first. =A0Won't it be good if validate get called automatically before
>>>> every method call so that they don't have to write more code and they
>>>> won't miss this validate?
>>>
>>> This may call for a back to roots approach, what exactly are you trying
>>> to accomplish, as in: what is the validation doing?
>>>
>>> perhaps if we see the full picture, we can recommend another perhaps
>>> more suited approach to the full thing, feel free to post the full code
>>> if you want / can, the more info the better!
>>>
>>> Regards,
>>>
>>> Nathan
>>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> Would this be better ...
>
> abstract class baseValidate() {
> =A0final public function __construct($params) {
> =A0// Determine if user is active.
> =A0// If OK, then call abstract function postConstruct($params)
> =A0}
>
> =A0abstract function postConstruct($params);
> }
>
>
> You can't override the constructor, so the validation will always be
> called. The developer's can implement their own postConstruct as if
> they where extending __construct.
>
>
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D2134=
74731
> ZOPA : http://uk.zopa.com/member/RQuadling
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php