2012-11-06 12 views
6

मैं एक PHP फ़ाइल मैं उपयोग कर रहा हूँ के साथ एक समस्या है, और मैं एक समाधान खोजने के लिए प्रतीत नहीं कर सकते हैं।

कोड में से एक भाग में $this->value के लिए मूल्य तय करना होगा और मान सही ढंग से मेरी परीक्षण के अनुसार निर्धारित है।

हालांकि, बाद में एक ही कोड में $this->value खाली है।

कोड यह रहा:

<?php 

class Padd_Input_Advertisement { 

    protected $keyword; 
    protected $value; 
    protected $name; 
    protected $description; 

    function __construct($keyword,$name,$description='') { 
     $this->keyword = $keyword; 
     $this->value = unserialize(get_option($keyword)); 
     $this->name = $name; 
     $this->description = $description; 
    } 

    public function get_keyword() { 
     return $this->keyword; 
    } 

    public function set_keyword($keyword) { 
     $this->keyword = $keyword; 
    } 

    public function get_value() { 
     return $this->value; 
    } 

    public function set_value($value) { 
     $this->value = $value; 
    } 

    public function get_name() { 
     return $this->name; 
    } 

    public function set_name($name) { 
     $this->name = $name; 
    } 

    public function get_description() { 
     return $this->description; 
    } 

    public function set_description($description) { 
     $this->description = $description; 
    } 

    public function __toString() { 

     $strHTML = ''; 
     $strHTML .= '<tr valign="top">'; 
     $strHTML .= ' <th scope="row"><label for="' . $this->keyword . '">' . $this->name . '</label></th>'; 
     $strHTML .= ' <td>'; 
     $strHTML .= '  <label for="' . $this->keyword. '_alt_desc">Short Description</label><br />'; 
     $strHTML .= '  <input name="' . $this->keyword . '_alt_desc" type="text" id="' . $this->keyword . '_alt_desc" value="' . $this->value->get_alt_desc() . '" size="80" /><br />'; 
     $strHTML .= '  <label for="' . $this->keyword. '_img_url">Image URL</label><br />'; 
     $strHTML .= '  <input name="' . $this->keyword . '_img_url" type="text" id="' . $this->keyword . '_img_url" value="' . $this->value->get_img_url() . '" size="80" /><br />'; 
     $strHTML .= '  <label for="' . $this->keyword. '_web_url">Website</label><br />'; 
     $strHTML .= '  <input name="' . $this->keyword . '_web_url" type="text" id="' . $this->keyword . '_web_url" value="' . $this->value->get_web_url() . '" size="80" />'; 
     $strHTML .= '  <br /><small>' . $this->description . '</small>'; 
     $strHTML .= ' </td>'; 
     $strHTML .= '</tr>'; 
     return $strHTML; 
    } 

} 

?> 

शीर्ष पर function __construct में, मूल्य सेट कर दिया जाता है, और मैं पुष्टि की है कि ऐसा होता है।

हालांकि, नीचे समारोह function __toString में, $this->value खाली है।

कोई विचार यह क्या हो सकता है?

संपादित

तो, संक्षेप में, $ this-> मूल्य ठीक से __construct समारोह में सेट है, लेकिन __toString समारोह में खाली है।

संपादित 2

मैं भी उल्लेख करना चाहिए कि अन्य चर कि __construct समारोह में सेट कर रहे हैं __toString समारोह में काम कर रहे हैं, $ this-> कीवर्ड की तरह। यह $ यह है-> मूल्य जो खाली हो रहा है।

संपादित 3 वर्ग इस

$padd_options['advertisements'] = array(
    new Padd_Input_Advertisement(
     PADD_NAME_SPACE . '_ads_125125_1', 
     'Square Ad 1 (125x125)', 
     'The advertisement will be posted at the side bar.' 
    ), 
    new Padd_Input_Advertisement(
     PADD_NAME_SPACE . '_ads_125125_2', 
     'Square Ad 2 (125x125)', 
     'The advertisement will be posted at the side bar.' 
    ), 
    new Padd_Input_Advertisement(
     PADD_NAME_SPACE . '_ads_125125_3', 
     'Square Ad 3 (125x125)', 
     'The advertisement will be posted at the side bar.' 
    ), 
    new Padd_Input_Advertisement(
     PADD_NAME_SPACE . '_ads_125125_4', 
     'Square Ad 4 (125x125)', 
     'The advertisement will be posted at the side bar.' 
    ), 
); 
+1

'__toString मूल्यांकन करने के लिए कोशिश कर रहा है' php में एक जादू विधि है क्या आप इसे किसी अन्य नाम में बदल सकते हैं और कोशिश कर सकते हैं? – coolguy

+1

खाली कैसे? 'Var_dump'' __toString' के अंदर क्या कहता है? – Ryan

+0

ठीक है, मैंने मूल्यों को एक .txt फ़ाइल में आउटपुट करने के लिए कुछ कोड डाला है।कोड के शीर्ष पर फ़ाइल के उचित मूल्य को फ़ाइल के नीचे लिखा गया था, केवल एक खाली फ़ाइल लिखी गई थी। –

उत्तर

1

मैं एक टिप्पणी के रूप में इस जोड़ लिया है की तरह कहा जाता है, लेकिन मुझे लगता है मैं न करने के लिए अभी तक प्रतिष्ठा अंक हैं।

क्या आपने त्रुटि रिपोर्टिंग चालू करने का प्रयास किया है?

error_reporting(E_ALL); 
ini_set("display_errors", 1); 
+0

हां, वास्तव में ब्याज की कोई बात नहीं। चेतावनी आम तौर पर नहीं दिखायी जाती है, और पृष्ठ पर घातक त्रुटियां पहले से दिखाई दे रही थीं। मुझे "घातक त्रुटि मिलती है: एक गैर-ऑब्जेक्ट पर सदस्य फ़ंक्शन get_alt_desc() पर कॉल करें, जिसे मैं संदेह कर रहा हूं वह वैरिएबल का परिणाम है जो रिक्त होने का उपयोग करने का प्रयास कर रहा है। –

+1

देखें कि जब आप ऐसा फ़ंक्शन बनाते हैं जो कुछ भी नहीं करता है लेकिन $ मान असाइन करता है तो क्या होता है। हो सकता है कि समय के लिए $ मूल्य सार्वजनिक बनाएं और देखें कि जिस वस्तु को आप $ मान में सहेजते हैं वह मौजूद है और उस फ़ंक्शन को कॉल करने के बाद Padd_Input_Advertisement के बाहर से पहुंच योग्य है। क्या आप Padd_Input_Advertisement के सही उदाहरण पर __toString को कॉल कर रहे हैं? – DudeOnRock

+0

यह वर्डप्रेस के लिए विषय का एक हिस्सा है। मुझे सच में यकीन नहीं है कि इनमें से किसी भी फ़ाइल को इस फ़ाइल के बाहर से कैसे पहुंचाया जाए। सुनिश्चित नहीं है कि कौन सी फाइलें जुड़ी हैं, आदि। मैं PHP वर्गों और सामानों के बारे में पूरी तरह से निश्चित नहीं हूं, लेकिन मेरा मानना ​​है कि __toString को Padd_Input_Advertisement –

1

ऑब्जेक्ट उन्मुख प्रोग्रामिंग में आपके पास $this का उपयोग करके कॉल फ़ंक्शन होना चाहिए। आपके फ़ंक्शन में __toString() आपने सीधे फ़ंक्शन कॉल किया है।

तो आप इस

$this->set_name(); 
+0

के प्रत्येक उदाहरण के लिए बुलाया जाता है, मेरे प्रश्न में "संपादित करें 3" देखें। –

+1

यह $ यह-> value-> get_alt_desc() क्या है? – Hkachhia

+0

एक अन्य फ़ाइल में एक और फ़ंक्शन जिसे एक मान वापस करने के लिए बुलाया जाता है। इसे $-> मान लेना चाहिए और इसे कुछ अलग करने के लिए get_alt_desc फ़ंक्शन के माध्यम से रखना चाहिए। लेकिन क्योंकि इस मूल्य में "मूल्य">> मूल्य खाली है, यह काम नहीं करता है। –

11

क्योंकि unserialized वस्तु नहीं रखने के तरीकों अगर वे वर्ग के प्रकार पता नहीं है करता है यह की तरह वर्ग के समारोह कहा जाता है।

जब आप get_keyword() फ़ंक्शन कॉल के परिणाम को बेअसर करते हैं, यदि धारावाहिक ऑब्जेक्ट की कक्षा ज्ञात नहीं है, तो PHP विशेष श्रेणी __PHP_Incomplete_Class_Name पर फ़ॉलबैक करेगा। यह वर्ग मूल रूप से एक डमी वर्ग है: इसमें कोई विधि नहीं है। हालांकि, यह आपको deserialized वस्तु के गुणों तक पहुंचने की अनुमति देता है।

तो, आप $this->value की विधि कॉल करने में सक्षम होना चाहते हैं (है जो आप __toString में कर रहे हैं), तो आप फ़ाइल जो unserialize कॉल करने से पहले $this->value के प्रकार वाणी शामिल करना होगा।

[संपादित करें] आप the PHP manual को अनसुलझा प्रक्रिया के बारे में अधिक जानकारी के लिए देख सकते हैं।

+0

स्क्रिप्ट के शीर्ष पर $-- मान का मूल्य है। तो अगर मैं वह मान लेना चाहता हूं, और get_alt_desc फ़ंक्शन के माध्यम से इसे चलाता हूं, तो मैं यह कैसे करूँगा? Get_alt_desc फ़ंक्शन इस तरह दिखता है: 'सार्वजनिक फ़ंक्शन get_alt_desc() { \t \t $ this-> alt_desc; \t} ' –

+0

और उस फ़ंक्शन में $-- alt_desc के पास एक मूल्य है जहां तक ​​मुझे पता है। –

+1

'__toString' विधि के अंदर' var_dump ($ this-> मान); 'का आउटपुट क्या है? यदि मैं सही हूं, तो आपको 'ऑब्जेक्ट (stdClass) # 2 (1) {...} 'जैसे कुछ देखना चाहिए। यदि ऐसा है, तो आपको बस उस फ़ाइल को शामिल करना है जो वास्तविक प्रकार की अनिश्चित वस्तु की घोषणा करता है। –

-2

तो, मैंने इस समस्या का हल निकाला है जो कोड को अपेक्षित रूप से काम करता है। यह मूल कारण को हल नहीं करता है, लेकिन यह एक अधिक कामकाज है।

__construct समारोह मैं जोड़ा में:

$this->value->get_alt_desc() 

$this->value2->get_alt_desc() 

करने के सभी तो:

$this->value2 = $this->value; 

और फिर कोड में त्रुटि मैं बदल पैदा कर रहा था कि कोड काम कर रहा था, उस चर के लिए $ यह-> मूल्य अपना मूल्य खो रहा था, और एक बार मैंने एक अलग चर का उपयोग किया था मुझे सबकुछ काम करना चाहिए जैसा कि होना चाहिए।

+0

तय किया है बहुत बुरा नहीं आपसे सहमत हैं .... इस समस्या से कोई लेना देना नहीं है – Baba

+0

इसलिए मेरे उत्तर में "वर्कअराउंड" शब्द। –

+0

-1, क्योंकि आपने समस्या को और भी भ्रम जोड़ने का फैसला किया है। –

1

Padd Solutions से Magaling विषय का उपयोग करना, यह सवाल की स्क्रिप्ट में शामिल है, मैं विषय की निर्देशिका में निम्न कोड index.php पर कहा:

/*********************************TEST***********************************/ 
    $padd_options[ 'advertisements' ] = array(new Padd_Input_Advertisement(PADD_THEME_SLUG . '_ads_468060_1', 'Banner Ad 1 (468x60)', 'The advertisement is placed beside the site name in the header.'), 
              new Padd_Input_Advertisement(PADD_THEME_SLUG . '_ads_468060_2', 'Banner Ad 2 (468x60)', 'The advertisement is placed just below the title in Search Result page, Categories page, Tags page, Author page, and Archives page.'), 
              new Padd_Input_Advertisement(PADD_THEME_SLUG . '_ads_125125_1', 'Square Ad 1 (125x125)', 'The advertisement will be posted at the side bar.'), 
              new Padd_Input_Advertisement(PADD_THEME_SLUG . '_ads_125125_2', 'Square Ad 2 (125x125)', 'The advertisement will be posted at the side bar.'), 
              new Padd_Input_Advertisement(PADD_THEME_SLUG . '_ads_125125_3', 'Square Ad 3 (125x125)', 'The advertisement will be posted at the side bar.'), 
              new Padd_Input_Advertisement(PADD_THEME_SLUG . '_ads_125125_4', 'Square Ad 4 (125x125)', 'The advertisement will be posted at the side bar.'),); 

    echo var_dump($padd_options[ 'advertisements' ]); 

/*********************************TEST***********************************/ 

और निम्न परिणाम है, जो दिखाता मूल्य है मिल गया कई अन्य मूल्यों के साथ एक सरणी:

array (size=6) 
    0 => 
    object(Padd_Input_Advertisement)[286] 
     protected 'keyword' => string 'magaling_ads_468060_1' (length=21) 
     protected 'value' => 
     object(Padd_Advertisement)[282] 
      private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-468x060.gif' (length=86) 
      private 'alt_desc' => string 'Padd Solutions' (length=14) 
      private 'web_url' => string 'http://www.paddsolutions.com' (length=28) 
      private 'target' => string '_new' (length=4) 
      private 'css_class' => string '' (length=0) 
     protected 'name' => string 'Banner Ad 1 (468x60)' (length=20) 
     protected 'description' => string 'The advertisement is placed beside the site name in the header.' (length=63) 
    1 => 
    object(Padd_Input_Advertisement)[284] 
     protected 'keyword' => string 'magaling_ads_468060_2' (length=21) 
     protected 'value' => 
     object(Padd_Advertisement)[283] 
      private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-468x060.gif' (length=86) 
      private 'alt_desc' => string 'Padd Solutions' (length=14) 
      private 'web_url' => string 'http://www.paddsolutions.com' (length=28) 
      private 'target' => string '_new' (length=4) 
      private 'css_class' => string '' (length=0) 
     protected 'name' => string 'Banner Ad 2 (468x60)' (length=20) 
     protected 'description' => string 'The advertisement is placed just below the title in Search Result page, Categories page, Tags page, Author page, and Archives page.' (length=131) 
    2 => 
    object(Padd_Input_Advertisement)[279] 
     protected 'keyword' => string 'magaling_ads_125125_1' (length=21) 
     protected 'value' => 
     object(Padd_Advertisement)[278] 
      private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-125x125.jpg' (length=86) 
      private 'alt_desc' => string 'Padd Solutions' (length=14) 
      private 'web_url' => string 'http://www.paddsolutions.com' (length=28) 
      private 'target' => string '_new' (length=4) 
      private 'css_class' => string '' (length=0) 
     protected 'name' => string 'Square Ad 1 (125x125)' (length=21) 
     protected 'description' => string 'The advertisement will be posted at the side bar.' (length=49) 
    3 => 
    object(Padd_Input_Advertisement)[277] 
     protected 'keyword' => string 'magaling_ads_125125_2' (length=21) 
     protected 'value' => 
     object(Padd_Advertisement)[276] 
      private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-125x125.jpg' (length=86) 
      private 'alt_desc' => string 'Padd Solutions' (length=14) 
      private 'web_url' => string 'http://www.paddsolutions.com' (length=28) 
      private 'target' => string '_new' (length=4) 
      private 'css_class' => string '' (length=0) 
     protected 'name' => string 'Square Ad 2 (125x125)' (length=21) 
     protected 'description' => string 'The advertisement will be posted at the side bar.' (length=49) 
    4 => 
    object(Padd_Input_Advertisement)[275] 
     protected 'keyword' => string 'magaling_ads_125125_3' (length=21) 
     protected 'value' => 
     object(Padd_Advertisement)[273] 
      private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-125x125.jpg' (length=86) 
      private 'alt_desc' => string 'Padd Solutions' (length=14) 
      private 'web_url' => string 'http://www.paddsolutions.com' (length=28) 
      private 'target' => string '_new' (length=4) 
      private 'css_class' => string '' (length=0) 
     protected 'name' => string 'Square Ad 3 (125x125)' (length=21) 
     protected 'description' => string 'The advertisement will be posted at the side bar.' (length=49) 
    5 => 
    object(Padd_Input_Advertisement)[217] 
     protected 'keyword' => string 'magaling_ads_125125_4' (length=21) 
     protected 'value' => 
     object(Padd_Advertisement)[216] 
      private 'img_url' => string 'http://localhost/TestSite/wp-content/themes/magaling/images/advertisement-125x125.jpg' (length=86) 
      private 'alt_desc' => string 'Padd Solutions' (length=14) 
      private 'web_url' => string 'http://www.paddsolutions.com' (length=28) 
      private 'target' => string '_new' (length=4) 
      private 'css_class' => string '' (length=0) 
     protected 'name' => string 'Square Ad 4 (125x125)' (length=21) 
     protected 'description' => string 'The advertisement will be posted at the side bar.' (length=49) 

मैं क्यों अपनी स्क्रिप्ट में मूल्य खो दिया है पता नहीं है, लेकिन वहाँ कुछ कमी या थीम में उपयोग कर रहे हैं या रास्ते में आप समारोह बुला रहे हैं में गलत है आपूर्ति की गई जानकारी एन है वैसे, काफी हद तक।

उम्मीद है कि इससे मदद मिलती है।

4

यह वही है आप समस्याओं के कारण होने वाली है:

$this->value = unserialize(get_option($keyword)); 

के रूप में कुछ अन्य लोगों के पहले से ही बताया है, तो आप अपने मूल पोस्ट get_option() समारोह की वास्तविक घोषणाओं और वर्ग कि get_alt_desc(), get_img_url(), get_web_url() तरीकों लपेटता साथ अद्यतन कर सकते हैं।

यह माना जा सकता है get_option() समारोह एक धारावाहिक वस्तु जो निम्नलिखित सार्वजनिक तरीकों को लागू करता है देता है कि:

public function get_alt_desc() { /* custom code */ } 
public function get_img_url() { /* custom code */ } 
public function get_web_url() { /* custom code */ } 

serialising और अपने वस्तु unserialising करके आप उपरोक्त सभी पद्धति के लिए उपयोग खो देते हैं। इससे बचने के लिए, आपको get_option() फ़ंक्शन को इसके क्रमबद्ध प्रतिनिधित्व के बजाय वास्तविक ऑब्जेक्ट को वापस करने के लिए संशोधित करने की आवश्यकता है।

मान लिया जाये कि वहाँ एक Value वर्ग जो आपके वस्तु का प्रतिनिधित्व करता है:

class Value { 
    protected $alt_desc; 
    protected $img_url; 
    protected $web_url; 

    public function __construct($keyword) { 
     $this->alt_desc = $keyword[0]; // some string build around $keyword 
     $this->img_url = $keyword[1]; // some string build around $keyword 
     $this->web_url = $keyword[2]; // some string build around $keyword 
    } 

    public function get_alt_desc() { 
     return $this->alt_desc; 
    } 

    public function get_img_url() { 
     return $this->img_url; 
    } 

    public function get_web_url() { 
     return $this->web_url; 
    } 
} 

तो फिर आप अपने get_option() समारोह को संशोधित कर सकता है बस एक वस्तु लौटने के लिए:

function get_option($keyword) { 
    /* 
    * The code below is just an example 
    * This function must return an object 
    */ 
    return new Value($keyword); 
} 

आपका Padd_Input_Advertisement निर्माता निम्नलिखित के रूप में अद्यतन किया जाना चाहिए :

function __construct($keyword,$name,$description='') { 
    $this->keyword = $keyword; 
    // removed the unserialize call as it's not necessary anymore 
    $this->value = get_option($keyword); 
    $this->name = $name; 
    $this->description = $description; 
} 
+0

सही विश्लेषण, समस्या के मूल कारण की पुष्टि करने के लिए हमें वास्तव में ओपी से अधिक जानकारी की आवश्यकता होगी। – Sven

+0

यह गलत है क्योंकि 'unserialize() 'वास्तविक वस्तु देता है (अधिक जानकारी के लिए इसके दस्तावेज़ देखें)। मध्यवर्ती 'मूल्य' वर्ग का उपयोग करना व्यर्थ है। –

1

संपादित करें: unserialise कॉल में बिल्कुल भी जरूरत है: यह के रूप में लंबे समय के रूप

$this->value = unserialize(get_option($keyword)); 

get_option पहले से ही unserialized ऑब्जेक्ट (यह update_option() द्वारा बचा लिया गया था - जो स्वचालित रूप से वस्तु यदि आवश्यक में सहेजने से पहले serialises डेटाबेस)

मूल जवाब

मुझे लगता है समस्या जिस तरह से आप वस्तु को क्रमानुसार के साथ है।

आपको यह सुनिश्चित करना होगा कि धारावाहिक प्रक्रिया (स्ट्रिंग प्रतिनिधित्व में रूपांतरण) क्रमबद्ध ऑब्जेक्ट का प्रकार ज्ञात है। केवल तभी आप सफलतापूर्वक (ऑब्जेक्ट पर स्ट्रिंग प्रस्तुति से वापस कनवर्ट करें) सफलतापूर्वक अनसुलझा कर सकते हैं।

_toString() के संबंध में आपके अन्य प्रश्न का उत्तर देने के लिए। इस फ़ंक्शन को PHP द्वारा जादुई रूप से कहा जाता है जब आप किसी स्ट्रिंग संदर्भ में अपनी ऑब्जेक्ट का उपयोग करने का प्रयास कर रहे होते हैं (जब आप ऑब्जेक्ट को उदाहरण के लिए गूंजने का प्रयास करते हैं)। आप इस फ़ंक्शन को सीधे कॉल नहीं करते हैं।

यहां एक ही PHP फ़ाइल है जो कार्य प्रवाह का प्रदर्शन करती है; उन हिस्सों पर टिप्पणियों के साथ जहां आपको जाल (समस्याओं के कारण) की तलाश करनी चाहिए। इसे index.php के रूप में सहेजें और परिणामों को देखने के लिए चलाएं। आपकी समस्या का

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 

class Padd_Input_Advertisement { 
    protected $keyword; 
    protected $value; 
    protected $name; 
    protected $description; 

    function __construct($keyword,$name,$description='') { 
     $this->keyword = $keyword; 
     $this->value = unserialize(get_option($keyword)); 
     $this->name = $name; 
     $this->description = $description;   
    } 

    public function get_keyword()   { return $this->keyword; } 
    public function set_keyword($keyword) { $this->keyword = $keyword; } 
    public function get_value()   { return $this->value; } 
    public function set_value($value)  { $this->value = $value; } 
    public function get_name()   { return $this->name; } 
    public function set_name($name)  { $this->name = $name; } 
    public function get_description()  { return $this->description;} 
    public function set_description($description) { $this->description = $description; } 
    public function __toString() { 

     $strHTML = ''; 
     $strHTML .= '<tr valign="top">'; 
     $strHTML .= ' <th scope="row"><label for="' . $this->keyword . '">' . $this->name . '</label></th>'; 
     $strHTML .= ' <td>'; 
     $strHTML .= '  <label for="' . $this->keyword. '_alt_desc">Short Description</label><br />'; 
     $strHTML .= '  <input name="' . $this->keyword . '_alt_desc" type="text" id="' . $this->keyword . '_alt_desc" value="' . $this->value->get_alt_desc() . '" size="80" /><br />'; 
     $strHTML .= '  <label for="' . $this->keyword. '_img_url">Image URL</label><br />'; 
     $strHTML .= '  <input name="' . $this->keyword . '_img_url" type="text" id="' . $this->keyword . '_img_url" value="' . $this->value->get_img_url() . '" size="80" /><br />'; 
     $strHTML .= '  <label for="' . $this->keyword. '_web_url">Website</label><br />'; 
     $strHTML .= '  <input name="' . $this->keyword . '_web_url" type="text" id="' . $this->keyword . '_web_url" value="' . $this->value->get_web_url() . '" size="80" />'; 
     $strHTML .= '  <br /><small>' . $this->description . '</small>'; 
     $strHTML .= ' </td>'; 
     $strHTML .= '</tr>'; 
     return $strHTML; 
    } 

} 

//Just a sample of the class representing your option object 
class Class_Of_Value { 
    protected $alt_desc; 
    protected $img_url; 
    protected $web_url; 

    public function __construct($keyword) { 
     $this->alt_desc = 'description'; 
     $this->img_url = 'image'; 
     $this->web_url = 'web'; 
    } 

    public function get_alt_desc() { return $this->alt_desc; } 
    public function get_img_url() { return $this->img_url; } 
    public function get_web_url() { return $this->web_url; } 
} 

//Sample of a CORRECT get_option function. 
function get_option($keyword) { 
    //THIS IS IMPORTANT 
    $valueObject = new Class_Of_Value($keyword); 
    //MAKE SURE THAT TYPE of serialised value is KNOWN 
    return serialize($valueObject); 
} 
//Here is the root cause of your problem. 
//Wordpress' get_option() DOES NOT serialise returned objects. It returns just value of an option pulled from database! 
//Hence you can't call methods $this->value->get_alt_desc() etc. 


define ('PADD_NAME_SPACE', ''); //dummy definition of the constant for the sake of PHP Notices 

$padd_options['advertisements'] = array(
    new Padd_Input_Advertisement(
     PADD_NAME_SPACE . '_ads_125125_1', 
     'Square Ad 1 (125x125)', 
     'The advertisement will be posted at the side bar.' 
    ), 
    new Padd_Input_Advertisement(
     PADD_NAME_SPACE . '_ads_125125_2', 
     'Square Ad 2 (125x125)', 
     'The advertisement will be posted at the side bar.' 
    ), 
    new Padd_Input_Advertisement(
     PADD_NAME_SPACE . '_ads_125125_3', 
     'Square Ad 3 (125x125)', 
     'The advertisement will be posted at the side bar.' 
    ), 
    new Padd_Input_Advertisement(
     PADD_NAME_SPACE . '_ads_125125_4', 
     'Square Ad 4 (125x125)', 
     'The advertisement will be posted at the side bar.' 
    ), 
); 

//Here __toString will be called by PHP outputing your HTML form filled with object values. 
echo($padd_options['advertisements'][0]); 

सुझाई समाधान (WP के update_option()) कि Wordpress विकल्प ठीक से (ऑब्जेक्ट प्रकार के साथ) धारावाहिक है सुनिश्चित करने के लिए करने से पहले डेटाबेस में सहेजा जा रहा है। तो, get_option serialized स्ट्रिंग को वापस कर देगा जो तब आपके __construct विधि में ठीक से अनसुलझा होगा।

+0

वस्तु प्रकार * नाम * हमेशा क्रमबद्ध है, iirc। हालांकि, प्रकार * घोषणा * अनिश्चितता से पहले उपलब्ध होना चाहिए। (जो आपने कहा है उसके विपरीत कौन सा है: निश्चित रूप से जब ऑब्जेक्ट का प्रकार सीरियलाइजिंग के समय जाना जाता है। अगर यह मामला नहीं होता तो यह तत्काल नहीं होता :) :) –

+0

हाँ आप सही हैं। लेकिन ऑब्जेक्ट प्रकार केवल तभी क्रमबद्ध होता है जब ऑब्जेक्ट स्वयं भी क्रमबद्ध होता है। जिस समस्या को मैंने रेखांकित किया है वह तथ्य यह है कि ऑब्जेक्ट को क्रमबद्ध नहीं किया जा सकता है। मैंने वर्डप्रेस कोडेक्स की जांच की और यह मुझे एक और जवाब का नेतृत्व किया। –

1

क्या आपने ऑपरेटर को मजबूर करने की कोशिश की -> प्राथमिकता?

मेरा मतलब है, इस से:

$this->value->get_alt_desc() 

यह करने के लिए:

($this->value)->get_alt_desc() 

शायद अपने PHP दुभाषिया() value->get_alt_desc()