Is it possible to declare a method static and nonstatic in PHP?

后端 未结 5 1467
北荒
北荒 2020-12-01 09:04

Can I declare a method in an object as both a static and non-static method with the same name that calls the static method?

I want to create a class that has a stati

相关标签:
5条回答
  • 2020-12-01 09:15

    You can do this, but it's a bit tricky. You have to do it with overloading: the __call and __callStatic magic methods.

    class test {
        private $text;
        public static function instance() {
            return new test();
        }
    
        public function setText($text) {
            $this->text = $text;
            return $this;
        }
    
        public function sendObject() {
            self::send($this->text);
        }
    
        public static function sendText($text) {
            // send something
        }
    
        public function __call($name, $arguments) {
            if ($name === 'send') {
                call_user_func(array($this, 'sendObject'));
            }
        }
    
        public static function __callStatic($name, $arguments) {
            if ($name === 'send') {
                call_user_func(array('test', 'sendText'), $arguments[0]);
            }
        }
    }
    

    This isn't an ideal solution, as it makes your code harder to follow, but it will work, provided you have PHP >= 5.3.

    0 讨论(0)
  • 2020-12-01 09:18

    I would make a hidden class as the constructor and return that hidden class inside the parent class that has static methods equal to the hidden class methods:

    // Parent class
    
    class Hook {
    
        protected static $hooks = [];
    
        public function __construct() {
            return new __Hook();
        }
    
        public static function on($event, $fn) {
            self::$hooks[$event][] = $fn;
        }
    
    }
    
    
    // Hidden class
    
    class __Hook {
    
        protected $hooks = [];
    
        public function on($event, $fn) {
            $this->hooks[$event][] = $fn;
        }
    
    }
    

    To call it statically:

    Hook::on("click", function() {});
    

    To call it dynamically:

    $hook = new Hook;
    $hook->on("click", function() {});
    
    0 讨论(0)
  • 2020-12-01 09:29

    No you can't have two methods with the same name. You could do basicly the same thing by renaming one of the methods. Renaming test::send("Hello World!"); to test::sendMessage("Hello World!"); would work. I would just create the a single send method with an optional text argument that changes how the method functions.

    public function send($text = false) {
        if (!$text) {
            $text = $this -> text;
        }
    
        // Send something
    }
    

    I courious as to why you need the static function at all.

    0 讨论(0)
  • 2020-12-01 09:33

    I agree that this should be avoided at all costs but there are some cases where it might be useful.

    In most cases it will just make your code unreadable and unmanageable.

    Believe me, I have been down that path.

    Here is an example with a use case scenario where it might still be practical.

    I am extending CakePHP 3.0's File class as my default file handling class.

    I wanted a to put in a static mime type guesser.

    In some cases I have a filename instead of an actual file and some assumptions need to be made in this case. ( if the file exists, try to get the mime from it else use extention of filename provided)

    Other times if I actually instantiated an object the default mime() method should work but if it fails the filename needs to be extracted from the object and the static method should be called instead.

    To avoid confusion my aim was to get the mime type by calling the same method:

    Static:

    NS\File::type('path/to/file.txt')
    

    As object

    $f = new NS\File('path/to/file.txt');
    $f->type();
    

    Here is my example extended class:

    <?php
    
    namespace NS;
    
    class File extends \Cake\Utility\File
    {
    
        public function __call($method, $args) {
            return call_user_func_array([get_called_class(), 'obj'.ucfirst($method)], $args);
        }
        public static function __callStatic($method, $args) {
            return call_user_func_array([get_called_class(), 'static'.ucfirst($method)], $args);
        }
    
        public function objType($filename=null){
            $mime = false;
            if(!$filename){
                $mime = $this->mime();
                $filename = $this->path;
            }
            if(!$mime){
                $mime = static::getMime($filename);
            }
            return $mime;
        }
    
        public static function staticType($filename=null){
            return static::getMime($filename);
        }
    
        public static function getMime($filename = null)
        {
            $mimes = [
                'txt' => 'text/plain',
                'htm' => 'text/html',
                'html' => 'text/html',
                'php' => 'text/html',
                'ctp' => 'text/html',
                'twig' => 'text/html',
                'css' => 'text/css',
                'js' => 'application/javascript',
                'json' => 'application/json',
                'xml' => 'application/xml',
                'swf' => 'application/x-shockwave-flash',
                'flv' => 'video/x-flv',
                // images
                'png' => 'image/png',
                'jpe' => 'image/jpeg',
                'jpeg' => 'image/jpeg',
                'jpg' => 'image/jpeg',
                'gif' => 'image/gif',
                'bmp' => 'image/bmp',
                'ico' => 'image/vnd.microsoft.icon',
                'tiff' => 'image/tiff',
                'tif' => 'image/tiff',
                'svg' => 'image/svg+xml',
                'svgz' => 'image/svg+xml',
                // archives
                'zip' => 'application/zip',
                'rar' => 'application/x-rar-compressed',
                'exe' => 'application/x-msdownload',
                'msi' => 'application/x-msdownload',
                'cab' => 'application/vnd.ms-cab-compressed',
                // audio/video
                'mp3' => 'audio/mpeg',
                'qt' => 'video/quicktime',
                'mov' => 'video/quicktime',
                // adobe
                'pdf' => 'application/pdf',
                'psd' => 'image/vnd.adobe.photoshop',
                'ai' => 'application/postscript',
                'eps' => 'application/postscript',
                'ps' => 'application/postscript',
                // ms office
                'doc' => 'application/msword',
                'rtf' => 'application/rtf',
                'xls' => 'application/vnd.ms-excel',
                'ppt' => 'application/vnd.ms-powerpoint',
                // open office
                'odt' => 'application/vnd.oasis.opendocument.text',
                'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
            ];
            $e = explode('.', $filename);
            $ext = strtolower(array_pop($e));
            if (array_key_exists($ext, $mimes)) {
                $mime = $mimes[$ext];
            } elseif (function_exists('finfo_open') && is_file($filename)) {
                $finfo = finfo_open(FILEINFO_MIME);
                $mime = finfo_file($finfo, $filename);
                finfo_close($finfo);
            } else {
                $mime = 'application/octet-stream';
            }
            return $mime;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 09:37

    Sorry for bumping an old thread, but I would like to expand on @lonesomeday 's answer. (Thanks @lonesomeday for the initial code sample.)

    I was also experimenting with this as well, but did not want to call the methods as he called them in the original post. Instead I have the following, which seems to work:

        class Emailer {
    
        private $recipient;
    
        public function to( $recipient )
        {
            $this->recipient = $recipient;
            return $this;
        }
    
        public function sendNonStatic()
        {
            self::mailer( $this->recipient );
        }
    
        public static function sendStatic( $recipient )
        {
            self::mailer( $recipient );
        }
    
        public function __call( $name, $arguments )
        {
            if ( $name === 'send' ) {
                call_user_func( array( $this, 'sendNonStatic' ) );
            }
        }
    
        public static function mailer( $recipient )
        {
            // send()
            echo $recipient . '<br>';
        }
    
        public static function __callStatic( $name, $arguments )
        {
            if ( $name === 'send' ) {
                call_user_func( array( 'Emailer', 'sendStatic' ), $arguments[0] );
            }
        }
    }
    
    Emailer::send( 'foo@foo.foo' );
    
    $Emailer = new Emailer;
    $Emailer->to( 'bar@bar.bar' );
    $Emailer->send();
    
    0 讨论(0)
提交回复
热议问题