0

在尝试使用 __toString() 时,我不断收到以下错误:

可捕获的致命错误:第 36 行的 C:wamp\www\demo.php 中的类地址对象无法转换为字符串

我哪里做错了?

演示.php

    echo '<h2>Instantiating Address</h2>';
    $address = new Address;

    echo '<h2>Empty Address</h2>';
    echo '<tt><pre>' . var_export($address, TRUE) . '</pre></tt>';

    echo '<h2>Setting properties...</h2>';
    $address->street_address_1 = '555 Fake Street';
    $address->city_name = 'Townsville';
    $address->subdivision_name = 'State';
    $address->postal_code = '12345';
    $address->country_name = 'United States of America';
    echo '<tt><pre>' . var_export($address, TRUE) . '</pre></tt>';

    echo '<h2>Displaying address ...</h2>';
    echo $address->display();

    echo '<h2>Testing magic __get and __set</h2>';
    unset($address->postal_code);
    echo $address->display();

    echo '<h2>Testing Address __construct with an array</h2>';
    $address_2 = new Address(array(
        'street_address_1' => '123 Phony Ave',
        'city_name' => 'Villageland',
        'subdivision_name' => 'Region',
        'postal_code' => '67890',
        'country_name' => 'Canada',
    ));
    echo $address_2->display();

    echo '<h2>Address __toString</h2>';
    echo $address_2;
?>

oop.php

<!DOCTYPE html>
<html>
<head><title>OOP</title></head>
    <body>
    <?php
        class Address {
            public $street_address_1;
            public $street_address_2;           
            public $city_name;
            public $subdivision_name;           
            public $country_name;

            protected $_postal_code;

            // Primary key of an address
            protected $_address_id;

            // When the recorded created and last updated
            protected $_time_created;
            protected $_time_updated;


            /*
                Constructor
                @param array data optional array of property names
            */
            function __construct($data = array()) {
                $this->_time_created = time();

                // Ensure that the address can be populated.
                if(!is_array($data)){
                    trigger_error('Unable to construct address with a ' . get_class($name));
                }
                // If there is at lease one value, populate the Address with it
                if(count($data) > 0) {
                    foreach($data as $name => $value){
                        // Special case for protected properties
                        if(in_array($name, array(
                            'time_created',
                            'time_updated',
                            ))){
                                $name = '_' . $name;
                            }
                            $this->$name = $value;
                    }
                }
            }


            /*
                Magic __get
                @param string $name
                @return mixed           
            */
            function __get($name){
                // Postal code lookup if unset.
                if (!$this->_postal_code){
                    $this->_postal_code = $this->_postal_code_guess();
                }

                /*
                    Magin __set.
                    @param string $name
                    @param mixed $value
                */
                function __set($name, $value){
                    // Allow anything to set the postal code.
                    if ('postal_code' == $name){
                        $this->$name = $value;
                        return;
                    }
                    // Unable to access property; trigger error,
                    trigger_error('Undefined or unallowed property via __set(): ' . $name);                 
                }

                /*
                    Magic __toString
                    @return string                  
                */

                function __toString() {
                    return $this->display();
                }

                $protected_property_name = '_' . $name;
                if(property_exists($this, $protected_property_name)){
                    return $this->$protected_property_name;
                }
                // unable to access property; trigger error
                trigger_error('Undefined_property via __get: ' . $name);
                return NULL;

            }

            /*
                Guess the postal code given the subdivision and city name.
                @todo Replace with a database lookup
                @return string
            */
            protected function _postal_code_guess(){
                return 'LOOKUP';
            }


            /*
                display an address in HTML
                return string.
            */
            function display(){
                $output = '';

                // Street address
                $output .= $this->street_address_1;
                if ($this->street_address_2){
                    $output .= '<br />' . $this->Street_address_2;
                }

                // City, Subdivision Postal
                $output .= '<br />';
                $output .= $this->city_name . ', ' . $this->subdivision_name;
                $output .= ' ' . $this->postal_code;

                // Country
                $output .= '<br />';
                $output .= $this->country_name;         

                return $output;             
            }
        }
    ?>
    </body>
</html>
4

1 回答 1

2

function __get()没有正确关闭。我已经删除了一些代码以使其工作。检查差异。

<?php
class Address {
    public $street_address_1;
    public $street_address_2;
    public $city_name;
    public $subdivision_name;
    public $country_name;

    protected $_postal_code;

    // Primary key of an address
    protected $_address_id;

    // When the recorded created and last updated
    protected $_time_created;
    protected $_time_updated;


    /*
        Constructor
        @param array data optional array of property names
    */
    function __construct($data = array()) {
        $this->_time_created = time();

        // Ensure that the address can be populated.
        if(!is_array($data)){
            trigger_error('Unable to construct address with a ' . get_class($name));
        }
        // If there is at lease one value, populate the Address with it
        if(count($data) > 0) {
            foreach($data as $name => $value){
                // Special case for protected properties
                if(in_array($name, array(
                    'time_created',
                    'time_updated',
                    ))){
                        $name = '_' . $name;
                    }
                    $this->$name = $value;
            }
        }
    }


    /*
        Magic __get
        @param string $name
        @return mixed
    */
    function __get($name){
        // Postal code lookup if unset.
        if (!$this->_postal_code){
            $this->_postal_code = $this->_postal_code_guess();
        }

        $protected_property_name = '_' . $name;

        if(property_exists($this, $protected_property_name)){
            return $this->$protected_property_name;
        }
        // unable to access property; trigger error
        trigger_error('Undefined_property via __get: ' . $name);
        return NULL;
    }

        /*
            Magin __set.
            @param string $name
            @param mixed $value
        */
        function __set($name, $value){
            // Allow anything to set the postal code.
            if ('postal_code' == $name){
                $this->$name = $value;
                return;
            }
            // Unable to access property; trigger error,
            trigger_error('Undefined or unallowed property via __set(): ' . $name);
        }

        /*
            Magic __toString
            @return string
        */

        function __toString() {
            return $this->display();
        }

    /*
        Guess the postal code given the subdivision and city name.
        @todo Replace with a database lookup
        @return string
    */
    protected function _postal_code_guess(){
        return 'LOOKUP';
    }


    /*
        display an address in HTML
        return string.
    */
    function display(){
        $output = '';

        // Street address
        $output .= $this->street_address_1;
        if ($this->street_address_2){
            $output .= '<br />' . $this->Street_address_2;
        }

        // City, Subdivision Postal
        $output .= '<br />';
        $output .= $this->city_name . ', ' . $this->subdivision_name;
        $output .= ' ' . $this->postal_code;

        // Country
        $output .= '<br />';
        $output .= $this->country_name;

        return $output;
    }
}
于 2013-04-18T09:17:37.457 回答