2012-11-21 12 views
7

असल में मैं कोड इग्निटर का उपयोग कर रहा हूं, और कोड इग्निटर बेस क्लास बहुत बड़ा है, जब मैं अपनी कुछ वस्तुओं को प्रिंट करता हूं तो उनके अंदर एम्बेडेड बेस क्लास होता है। इससे मुझे वह जानकारी मिलती है जो मैं वास्तव में चाहता था (शेष गुण)।print_r या ऑब्जेक्ट से निजी संपत्ति को बाहर कर दें?

तो, मुझे आश्चर्य है कि क्या कोई रास्ता है जिसे मैं छुपा सकता हूं, या बेस क्लास ऑब्जेक्ट को हटा सकता हूं?

मैं

clone $object; 
unset($object->ci); 
print_r($object); 

की कोशिश की है लेकिन निश्चित रूप से ci संपत्ति निजी है।

वास्तविक समारोह मैं डंपिंग के लिए उपयोग कर रहा हूँ है:

/** 
* Outputs the given variables with formatting and location. Huge props 
* out to Phil Sturgeon for this one (http://philsturgeon.co.uk/blog/2010/09/power-dump-php-applications). 
* To use, pass in any number of variables as arguments. 
* Optional pass in "true" as final argument to kill script after dump 
* 
* @return void 
*/ 
function dump() { 
    list($callee) = debug_backtrace(); 
    $arguments = func_get_args(); 
    $total_arguments = count($arguments); 
    if (end($arguments) === true) 
     $total_arguments--; 

    echo '<fieldset style="background: #fefefe !important; border:2px red solid; padding:5px">'; 
    echo '<legend style="background:lightgrey; padding:5px;">' . $callee['file'] . ' @ line: ' . $callee['line'] . '</legend><pre>'; 

    $i = 0; 
    foreach ($arguments as $argument) { 
     //if the last argument is true we don't want to display it. 
     if ($i == ($total_arguments) && $argument === true) 
      break; 

     echo '<br/><strong>Debug #' . (++$i) . ' of ' . $total_arguments . '</strong>: '; 

     if ((is_array($argument) || is_object($argument)) && count($argument)) { 
      print_r($argument); 
     } else { 
      var_dump($argument); 
     } 
    } 

    echo '</pre>' . PHP_EOL; 
    echo '</fieldset>' . PHP_EOL; 

    //if the very last argument is "true" then die 
    if (end($arguments) === true) 
     die('Killing Script'); 
} 

उत्तर

3

यह अज्ञात वर्ग के लिए केवल सार्वजनिक वार्स लौटने के लिए काम करना चाहिए:

// Get the class of the object 
$class_of_object= get_class($object); 

// Get only public attributes (Note: if called from within class will return also private and protected) 
$clone = get_class_vars($class_of_object); 

// Try it 
dump($clone); 

और यह काफी hacky है, लेकिन काम करता है - करने के लिए ऑब्जेक्ट से निजी संपत्ति को हटाएं (यह ऑब्जेक्ट नाम को संरक्षित नहीं करेगा) और निश्चित रूप से एक और दोष यह है कि आपको हार्ड कोड संपत्ति नाम की आवश्यकता है:

// First cast clone to array 
$b = (array) clone($a); 

// Then unset value (There will be null bytes around A and to use them you need to run it in double quotes 
// Replace A for * to remove protected properties 
unset($b["\0A\0_ci"]); 

// Finally back to object 
$b = (object) $b; 

// Test it 
dump($b); 
+0

लेकिन मैं अन्य निजी संपत्तियों चाहते हैं, मैं तो बस @Hailwood है कि एक संपत्ति – Hailwood

+0

अब थोड़े hacky तरह से प्रयास करते हैं, नहीं चाहते हैं, लेकिन हो सकता है काम। – arma

+0

लेकिन 'get_object_vars() 'ऑब्जेक्ट को पैरामीटर के रूप में अपेक्षा करता है। '$ class_of_object' एक स्ट्रिंग है। – TheFox

1

आप पीएचपी Reflextion एपीआई

$myClassName = 'myChildClass'; 
$reflection = new ReflectionClass($myClassName); 

// get properties, only public in this case 
$properties = $reflection->getProperties(ReflectionMethod::IS_PUBLIC); 
//Print all properties (including parent class) 
print_r($properties); 

//Print properties of desired class only 
foreach ($properties as $property) { 
    if ($property->class == $myClassName) { 
     print_r($property); 
    } 
} 

तरीकों के लिए एक ही बात के साथ आसानी से में कर सकते हैं।

यदि आपको वास्तव में इसकी आवश्यकता है - तो आप इस काम को करने के लिए विशेष कार्य बना सकते हैं और आपको ऐसे विश्लेषण की आवश्यकता होने पर कॉल कर सकते हैं। लेकिन मुझे लगता है कि ज्यादातर मामलों में अच्छा आईडीई, मेरे पसंदीदा PHPStorm की तरह, यह आपके लिए यह काम कर सकता है और जब आप क्लास इंस्टेंस को कॉल करते हैं - आपको केवल सुझाव सूची में सार्वजनिक तरीके दिखाते हैं।

+0

होना चाहिए, मैं PHPStorm देखभाल का भी उपयोग करता हूं ताकि यह जानकारी हो सके कि PHPStorm आपके लिए यह कैसे करता है? – Hailwood

+0

ठीक है, जब आप $ obj टाइप करते हैं -> [ctrl + Enter] यह केवल निजी विधियों या गुणों को नहीं दिखाता है। – Pavel

0

मैं एक आसान तरीका है कि मेरे लिए ठीक काम करता है है:

<?php print_r(json_decode(json_encode($object))); ?>