मैं मानता हूँ कि यह हर हालत में बचा जाना चाहिए, लेकिन कुछ मामलों में जहां यह उपयोगी हो सकता है कर रहे हैं: इसके बजाय मैं निम्नलिखित है, जो लगता है काम करना है।
ज्यादातर मामलों में यह आपके कोड को अपठनीय और अप्रबंधनीय बना देगा।
मेरा विश्वास करो, मैं उस रास्ते से नीचे गया हूं।
यहां एक केस केस परिदृश्य के साथ एक उदाहरण है जहां यह अभी भी व्यावहारिक हो सकता है।
मैं केकपीएचपी 3.0 की फाइल क्लास को अपनी डिफ़ॉल्ट फ़ाइल हैंडलिंग क्लास के रूप में विस्तारित कर रहा हूं।
मैं एक स्थिर माइम प्रकार अनुमानक को रखना चाहता था।
कुछ मामलों में मैं एक फ़ाइल नाम के बजाय एक वास्तविक फ़ाइल है और कुछ मान्यताओं इस मामले में किए जाने के लिए की जरूरत है।(यदि फ़ाइल मौजूद है, तो इसके द्वारा माइम प्राप्त करने का प्रयास करें और प्रदान किए गए फ़ाइल नाम के विस्तार का उपयोग करें)
अन्य बार यदि मैं वास्तव में किसी ऑब्जेक्ट को तुरंत चालू करता हूं तो डिफ़ॉल्ट माइम() विधि को काम करना चाहिए, लेकिन अगर यह विफल हो जाता है तो फ़ाइल नाम निकालने की आवश्यकता होती है ऑब्जेक्ट से और स्थैतिक विधि को इसके बजाय बुलाया जाना चाहिए।
स्टेटिक:
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;
}
}
यदि मैं पूछूं तो आप ऐसा क्यों करना चाहेंगे? – PeeHaa
[क्या स्थिर सदस्य नॉनस्टैटिक सदस्यों का उपयोग कर सकते हैं और इसके विपरीत?] (Http://stackoverflow.com/questions/2204128/could-static-members-use-nonstatic-members-and-vice-versa) –
उत्तर कोई btw नहीं है , लेकिन मैं वास्तव में उत्सुक हूं कि आपको ऐसा क्यों लगता है कि आप ऐसा करना चाहते हैं। – PeeHaa