在PHP中可以声明一个方法的static和nonstatic吗?

nvbavucw  于 2023-06-28  发布在  PHP
关注(0)|答案(7)|浏览(113)

我可以在一个对象中声明一个方法作为一个静态和非静态的方法,并使用相同的名称调用静态方法吗?
我想创建一个类,它有一个静态方法“send”和一个调用静态函数的非静态方法。例如:

class test {
    private $text;
    public static function instance() {
        return new test();
    }

    public function setText($text) {
        $this->text = $text;
        return $this;
    }

    public function send() {
        self::send($this->text);
    }

    public static function send($text) {
        // send something
    }
}

我希望能够调用这两个函数

test::send("Hello World!");

test::instance()->setText("Hello World")->send();

可能吗?

oymdgrw7

oymdgrw71#

你可以做到这一点,但这有点棘手。你必须用重载来做:__call__callStatic魔术方法。

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]);
        }
    }
}

这不是一个理想的解决方案,因为它使您的代码更难理解,但它会工作,只要您有PHP >= 5.3。

5m1hhzi4

5m1hhzi42#

我将创建一个隐藏类作为构造函数,并返回父类中的隐藏类,该父类具有与隐藏类方法相等的静态方法:

// 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;
    }

}

静态调用它:

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

动态调用它:

$hook = new Hook;
$hook->on("click", function() {});
gblwokeq

gblwokeq3#

不能有两个同名的方法。你可以通过重命名其中一个方法来做基本相同的事情。将test::send("Hello World!");重命名为test::sendMessage("Hello World!");即可。我只需要创建一个single send方法,并使用一个可选的text参数来改变方法的功能。

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

    // Send something
}

我想知道为什么你需要静态功能。

yfjy0ee7

yfjy0ee74#

我同意应该不惜一切代价避免这种情况,但在某些情况下,这可能是有用的。
在大多数情况下,它只会使您的代码不可读和不可管理。
相信我,我也经历过。
下面是一个用例场景的例子,在这个场景中它可能仍然是实用的。
我正在扩展CakePHP 3.0的File类作为我的默认文件处理类。
我想把一个静态哑剧类型的猜测。
在某些情况下,我有一个文件名,而不是一个实际的文件和一些假设需要在这种情况下作出。(如果文件存在,请尝试从中获取MIME,否则请使用提供的文件名扩展名)
其他时候,如果我实际上示例化了一个对象,默认的mime()方法应该可以工作,但是如果它失败了,则需要从对象中提取文件名,并且应该调用静态方法。
为了避免混淆,我的目标是通过调用相同的方法来获取mime类型:

静态:

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

作为对象

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

以下是我的扩展类示例:

<?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;
    }
}
8oomwypt

8oomwypt5#

在php中,你可以设置/分配一个类方法,方法可见性(Public,Private,Protected),也可以声明类方法或类属性的限制分布,比如那些可以在类外部访问的。
为了呼叫的目的,我们有两种方法,

  1. Static(self::)
    1.非静态($this->)
    让我们拿一个类Foo有一些方法和属性..具有可见性和呼叫方法
<?php

    class Foo {

     public const WELCOME ='This is WELCOME Non Static Constant For Foo Class';
    public string $text='This is A Text Non Static Foo Class Properties';
    public static string $texter='This is A Texter Foo Static Class Properties';
    private string $ptext='This is a private string Non Static properties of Class Foo';
      
    
    public static function Bar()
    {
        echo "Static Method Bar is calling\n";
    }
    
    public function Baz()
    {
        echo "Non Static Method Baz is calling \n";
    }
    
    
    protected function Another()
    {
        echo "Non Static Method Another is calling \n";
    }
    
    private function Again()
    {
        echo "Non Static Private Method Again is calling \n";
    }
    
    protected static function AnotherOne()
    {
        echo "Non Static Method Another is calling \n";
    }
    
    private static function AgainOne()
    {
        echo "Non Static Private Method Again is calling \n";
    }
    
    
    public static function bypass()
    {
        return self::AgainOne();
    }
    
    public function getPText()
    {
        return $this->ptext;
    }
    
    
    
}
?>

现在测试这个类

<?php

//Non Static Call By Creating an $app instance of Foo Class..
$app = new Foo();
 echo $app->WELCOME;        // Undefined property: Foo::$WELCOME
 echo $app->text;           // This is A Text Non Static Foo Class Properties
 echo $app->texter;         // Accessing static property Foo::$texter as non static
 echo $app->Bar();          // Static Method Bar is calling
 echo $app->Baz();          // Non Static Method Baz is calling 
 echo $app->Another();      // Uncaught Error: Call to protected method Foo::Another() from global scope
 echo $app->Again();        // Uncaught Error: Call to private method Foo::Again() from global scope
 echo $app->AnotherOne();   // Uncaught Error: Call to protected method Foo::AnotherOne() from global scope
 echo $app->AgainOne();     // Uncaught Error: Call to private method Foo::AgainOne() from global scope
 echo $app->bypass();       // Non Static Private Method Again is calling 
 echo $app->ptext;          // Uncaught Error: Cannot access private property Foo::$ptext
 echo $app->getPText();     // This is a private string Non Static properties of Class Foo 

//Static Call
 echo Foo::WELCOME;         // This is WELCOME Non Static Constant For Foo Class
 echo Foo::text;            // Uncaught Error: Undefined constant Foo::text
 echo Foo::texter;          // Uncaught Error: Undefined constant Foo::texter
 echo Foo::Bar();           // Static Method Bar is calling
 echo Foo::Baz();           // Uncaught Error: Non-static method Foo::Baz() cannot be called statically
 echo Foo::Another();       // Uncaught Error: Call to protected method Foo::Another() from global scope
 echo Foo::Again();         // Uncaught Error: Call to private method Foo::Again() from global scope 
 echo Foo::AnotherOne();    // Uncaught Error: Call to protected method Foo::AnotherOne() from global scope
 echo Foo::AgainOne();      // Uncaught Error: Call to private method Foo::AgainOne() from global scope
 echo Foo::bypass();        // Non Static Private Method Again is calling 
 
 ?>

请参见In Action here

0s0u357o

0s0u357o6#

删除E_STRICT错误日志的另一个选项如下所示:

<?php 
error_reporting(E_ALL ^ E_STRICT);
class userData {

    private static $id = 33;
    public function staticCall() {
      
        if(isset($this)) {
            //object
            echo 'object:';
        } else {
            //static //made STRICT ERROR
            echo 'static:';
        }
        return self::$id;
    }
    
}
  
  $obj = new userData();
  print_r($obj->staticCall()); // object:33
  print_r(userData::staticCall()); // static:33

?>

3zwtqj6y

3zwtqj6y7#

对不起,我撞到了一个老主题,但我想扩展一下@lonesomeday的答案。(感谢@lonesomeday提供的初始代码示例。
我也在尝试这个,但不想像他在原始帖子中那样调用这些方法。我有下面的代码,* 似乎 * 工作:

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();

相关问题