2013-12-16 7 views
6

method_exists के साथ, यह मूल वर्ग समेत सभी विधियों की जांच करता है।जांचें कि कोई विस्तारित कक्षा में कोई विधि मौजूद है लेकिन माता-पिता वर्ग

उदाहरण:

class Toot { 
    function Good() {} 
} 

class Tootsie extends Toot { 
    function Bad() {} 
} 

function testMethodExists() { 
    // true 
    var_dump(method_exists('Toot', 'Good')); 

    // false 
    var_dump(method_exists('Toot', 'Bad')); 

    // true 
    var_dump(method_exists('Tootsie', 'Good')); 

    // true 
    var_dump(method_exists('Tootsie', 'Bad')); 
} 

मैं कैसे जांच कर सकते हैं कि विधि केवल वर्तमान वर्ग पर मौजूद है और नहीं माता पिता वर्ग (यानी Tootsie।)?

+0

प्रतिबिंब का उपयोग करें! – user2907171

+0

@ user2907171 यह एक दिलचस्प विकल्प की तरह दिखता है, क्या आप अधिक विस्तृत उत्तर प्रदान कर सकते हैं? – xiankai

उत्तर

2

आप अधिक जानकारी ReflectionClass

+2

दुर्भाग्यवश ऐसा लगता है कि यह 'method_exists' के समान समस्या से पीड़ित है - "माता-पिता विधियां (दृश्यता के बावजूद) एक प्रतिबिंब ऑब्जेक्ट के लिए भी उपलब्ध हैं।" http://ca3.php.net/manual/en/reflectionclass.hasmethod.php#100660 से – xiankai

6

के लिए प्रतिबिंब

$refl = new ReflectionClass($class_name); 

if($refl->hasMethod($method_name)){ 
     //do your stuff here 
} 

उपयोग कर सकते हैं वी के बाद से। 4.0.5 php get_parent_class() विधि, कि माता पिता वर्ग रिटर्न है। तो आप इसे relflection बिना संभाल कर सकते हैं:

class A 
{ 
    function a() { /* ... */}  
    function b() { /* ... */}  
} 
class B extends A 
{ 
    function b() { /* ... */}  
    function c() { /* ... */}  
} 

function own_method($class_name, $method_name) 
{  
    if (method_exists($class_name, $method_name)) 
    { 
     $parent_class = get_parent_class($class_name); 
     if ($parent_class !== false) return !method_exists($parent_class, $method_name); 
     return true; 
    } 
    else return false; 
} 

var_dump(own_method('B', 'a')); // false 
var_dump(own_method('B', 'b')); // false 
var_dump(own_method('B', 'c')); // true 
0

मेरे लिए सवाल चेक एक विधि एक विस्तारित कक्षा में मौजूद है, लेकिन अगर नहीं था माता पिता वर्गमाता पिता वर्ग

से ऊपर के उदाहरण ले रहा है से Elantcev मिखाइल, यह इस तरह और अधिक दिखाई देगा: -

class A 
{ 
     function a() { /* ... */}  
     function b() { /* ... */}  
     public function own_method($method_name) 
     {  
      if (method_exists($this, $method_name)) 
      { 
       return true; 
      } 
      else return false; 
     } 
} 
class B extends A 
{ 
    function b() { /* ... */}  
    function c() { /* ... */}  
} 

$b = new B; 

var_dump($b->own_method('c')); // true 

आप ऐसा क्यों करना चाहते हैं?

मैं एक वर्ग BaseModel है जिसमें विस्तारित मॉडल में विशेषताओं के लिए मैं प्रारूप संकेत:

class BaseModel 
{ 
    public function attributeHelp($attributeName) 
    { 
     if (method_exists($this, 'hints') && array_key_exists($attributeName, $this->hints())) 
     { 
      return '<i html for nicely formatted hint' 
       . $this->hints()[$attributeName] 
       . '"></i>'; 
     } 
     return false; 
    } 
} 

और फिर मेरे डेटा मॉडल में

class Customer extends BaseModel 
{ 
    public function hints() 
    { 
     return [ 
      'customer_name' => 'please use your fullname ...', 
      'some other attribute' => 'some other description' 
     ]; 
    } 
} 
0

आप जानना चाहते हैं कि विधि मौजूद है की जरूरत है किसी दिए गए बच्चे में माता-पिता वर्ग में अस्तित्व के बावजूद आप इसे ऐसा कर सकते हैं:

$reflectionClass = new ReflectionClass($class_name); 
if ($reflectionClass->getMethod($method_name)->class == $class_name) { 
    // do something 
} 
संबंधित मुद्दे