Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可以在PHP中声明静态和非静态方法吗?_Php_Oop - Fatal编程技术网

可以在PHP中声明静态和非静态方法吗?

可以在PHP中声明静态和非静态方法吗?,php,oop,Php,Oop,我可以将对象中的方法声明为静态方法和非静态方法,并使用调用静态方法的相同名称吗 我想创建一个类,它有一个静态方法“send”和一个调用静态函数的非静态方法。例如: class test { private $text; public static function instance() { return new test(); } public function setText($text) { $this->text =

我可以将对象中的方法声明为静态方法和非静态方法,并使用调用静态方法的相同名称吗

我想创建一个类,它有一个静态方法“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
    }
}
我希望能够调用这两个was上的函数

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

有可能吗?

你可以这样做,但有点棘手。你必须使用重载:神奇的方法

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,它会工作。

不,您不能有两个同名的方法。您可以通过重命名其中一个方法来做基本相同的事情。重命名
test::send(“helloworld!”)
测试::发送消息(“你好,世界!”)将起作用。我只需要创建一个带有可选文本参数的单一send方法,该参数可以更改该方法的工作方式

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

    // Send something
}

我很清楚为什么您需要静态函数。

很抱歉,我遇到了一个老问题,但我想进一步介绍一下@lonesomeday的答案。(感谢@lonesomeday提供的初始代码示例。)

我也在尝试这个方法,但不想像他在原始帖子中所说的那样调用这些方法。相反,我有以下几点似乎是可行的:

类电子邮件发送程序{
私人美元接受者;
公共职能部门(收件人)
{
$this->recipient=$recipient;
退还$this;
}
公共函数sendnostatic()
{
self::mailer($this->recipient);
}
公共静态函数sendStatic($recipient)
{
self::mailer($recipient);
}
公共函数调用($name,$arguments)
{
如果($name=='send'){
调用用户函数(数组($this,'sendNonStatic');
}
}
公共静态函数mailer($recipient)
{
//发送()
回显$recipient。“
”; } 公共静态函数\uuu callStatic($name,$arguments) { 如果($name=='send'){ 调用_user_func(数组('Emailer','sendStatic'),$arguments[0]); } } } Emailer::发送('foo@foo.foo' ); $Emailer=新的Emailer; $Emailer->to($Emailer)bar@bar.bar' ); $Emailer->send();
我同意应该不惜一切代价避免这种情况,但在某些情况下,这可能是有用的

在大多数情况下,它只会使您的代码无法阅读和管理

相信我,我一直在走这条路

下面是一个用例场景的示例,在该场景中它可能仍然是实用的

我将扩展CakePHP3.0的文件类作为默认的文件处理类

我想要一个静态mime类型猜测器

在某些情况下,我有一个文件名而不是一个实际的文件,在这种情况下需要做一些假设。(如果文件存在,请尝试从中获取mime,否则请使用提供的文件名扩展名)

其他时候,如果我实际实例化了一个对象,默认的mime()方法应该可以工作,但如果失败,则需要从对象中提取文件名,并调用静态方法

为了避免混淆,我的目标是通过调用相同的方法来获取mime类型:

静态:

NS\File::type('path/to/file.txt')
<?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;
    }
}
作为对象

$f = new NS\File('path/to/file.txt');
$f->type();
这是我的扩展类示例:

NS\File::type('path/to/file.txt')
<?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;
    }
}
“应用程序/xml”,
“swf”=>“应用程序/x-shockwave-flash”,
“flv”=>“视频/x-flv”,
//图像
'png'=>'image/png',
“jpe”=>“图像/jpeg”,
'jpeg'=>'图像/jpeg',
'jpg'=>'image/jpeg',
'gif'=>'image/gif',
'bmp'=>'image/bmp',
“ico'=>“image/vnd.microsoft.icon”,
“tiff”=>“图像/tiff”,
“tif”=>“图像/tiff”,
“svg'=>“图像/svg+xml”,
'svgz'=>'image/svg+xml',
//档案
“zip”=>“应用程序/zip”,
“rar”=>“应用程序/x-rar-compressed”,
“exe”=>“应用程序/x-msdownload”,
“msi”=>“应用程序/x-msdownload”,
“cab'=>“应用程序/vnd.ms cab压缩”,
//音频/视频
“mp3”=>“音频/mpeg”,
“qt'=>“视频/快速时间”,
'mov'=>'video/quicktime',
//土坯
'pdf'=>'应用程序/pdf',
“psd”=>“image/vnd.adobe.photoshop”,
“ai”=>“应用程序/postscript”,
“eps”=>“应用程序/postscript”,
“ps'=>“应用程序/postscript”,
//微软办公室
'doc'=>'应用程序/msword',
“rtf”=>“应用程序/rtf”,
“xls”=>“应用程序/vnd.ms excel”,
'ppt'=>'应用程序/vnd.ms powerpoint',
//开放办公室
'odt'=>'应用程序/vnd.oasis.opendocument.text',
'ods'=>'应用程序/vnd.oasis.opendocument.spreadsheet',
];
$e=分解('.',$filename);
$ext=strtolower(数组_pop($e));
如果(数组键存在($ext,$mimes)){
$mime=$mimes[$ext];
}elseif(函数存在('finfo_open')&&is_文件($filename)){
$finfo=finfo\u open(FILEINFO\u MIME);
$mime=finfo_文件($finfo,$filename);
finfo_close($finfo);
}否则{
$mime='应用程序/八位字节流';
}
返回$mime;
}
}

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

// 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() {});

如果我可以问的话,你为什么要这样做呢?很多人热衷于把这当作一个糟糕的问题来驳斥,只是因为他们个人认为它没有什么用处。这里有一个例子:我之所以需要它,是因为我想为事件处理提供一些函数。我想提供一些课程