Tôi đồng ý rằng điều này cần phải tránh bằng mọi giá nhưng có một số trường hợp nó có thể có ích.
Trong hầu hết các trường hợp, nó sẽ chỉ làm cho mã của bạn không đọc được và không thể quản lý được.
Tin tôi đi, tôi đã đi xuống con đường đó.
Dưới đây là ví dụ về trường hợp sử dụng trong trường hợp vẫn có thể thực tế.
Tôi đang mở rộng lớp Tệp của CakePHP 3.0 làm lớp xử lý tệp mặc định của tôi.
Tôi muốn đặt trong một trình đoán kiểu tĩnh.
Trong một số trường hợp, tôi có tên tệp thay vì tệp thực và một số giả định cần phải được thực hiện trong trường hợp này.(nếu tập tin tồn tại, hãy thử để có được các mime từ nó khác sử dụng extention của tên tập tin được cung cấp)
lần khác nếu tôi thực sự instantiated một đối tượng mặc định mime() phương pháp nên làm việc, nhưng nếu nó không tên tập tin cần phải được trích xuất từ đối tượng và phương thức tĩnh nên được gọi thay thế.
Để tránh nhầm lẫn mục tiêu của tôi là để có được những loại mime bằng cách gọi phương pháp tương tự:
tĩnh:
NS\File::type('path/to/file.txt')
Như đối tượng
$f = new NS\File('path/to/file.txt');
$f->type();
Đây là lớp mở rộng ví dụ của tôi:
<?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;
}
}
Tại sao bạn muốn làm điều đó nếu tôi có thể yêu cầu? – PeeHaa
[thành viên tĩnh có thể sử dụng thành viên không phải là thành viên không và ngược lại?] (Http://stackoverflow.com/questions/2204128/could-static-members-use-nonstatic-members-and-vice-versa) –
Câu trả lời là không có btw , nhưng tôi thực sự tò mò là tại sao bạn nghĩ bạn muốn làm điều đó. – PeeHaa