How to Implement event listening for method calls of a defined class in event-php

Source: Internet
Author: User
Tags traits
{Code ...} if you want to implement the implementation without modifying the existing call Process Code, define the class method (here is & #039; mycall & #039 ;) event listening, such as intercepting and modifying the return of mycall. I know that you can solve the problem through a similar method of the packaging class, but if you do not want to modify the existing call process...
class a{    function mycall($var){        return 'hello:'.$var;    }}//invoke$aobj = new a();echo $aobj->mycall('abc');

To implementDo not modify the existing call ProcessIn the case of code, to listen to events that have defined class methods (here is 'mycall'), suchIntercepts and modifies the return of mycall..

I know that you can solve the problem through a similar method of the packaging class. But if you don't want to modify the existing calling process, just add code before or after the class definition to solve the problem? In addition, the system environment is 5.3, so traits cannot be used for the time being. Is there any work und?

Reply content:
class a{    function mycall($var){        return 'hello:'.$var;    }}//invoke$aobj = new a();echo $aobj->mycall('abc');

To implementDo not modify the existing call ProcessIn the case of code, to listen to events that have defined class methods (here is 'mycall'), suchIntercepts and modifies the return of mycall..

I know that you can solve the problem through a similar method of the packaging class. But if you don't want to modify the existing calling process, just add code before or after the class definition to solve the problem? In addition, the system environment is 5.3, so traits cannot be used for the time being. Is there any work und?

So?

trait T {    private function mycall($var){        return 'hello:'.str_pad($var, 4, '__', STR_PAD_LEFT);    }}//invokeclass  A{    use T {        mycall as traitmycall;    }    public function mycall($val)    {        return $this->traitmycall($val);    }}$obj = new A();echo $obj->mycall('abc');

class a{    function mycall($var)    {        $msg = 'hello:' . $var;        $msg = $this->aftermycall($msg);        return $msg;    }    function aftermycall($var)    {        $var .= 'aftermycall';        return $var;    }}$aobj = new a();echo $aobj->mycall('abc');

Pay attention to priority when using Traits

class a{    function mycall($var)    {        $msg = 'hello:' . $var;        $msg = $this->aftermycall($msg);        return $msg;    }    function __call($method, $var)    {        echo __LINE__, PHP_EOL;        // ....    }}$aobj = new a();echo $aobj->mycall('abc');

No tests

The magic method is easy to implement. You only need to set the class to be listened to as private. class a {private function mycall ($ var) {return 'Hello :'. $ var;} function _ call ($ method, $ var) {if ($ method = 'method to listen ') {// handle $ var // call $ this-> $ method ($ var) // process the returned results and return} else {// other private methods that do not need to be listened to} $ aobj = new (); echo $ aobj-> mycall ('abc ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.