oop - PHP toString not printing -
i keep getting following error while trying using __tostring():
catchable fatal error: object of class address not converted string in c:wamp\www\demo.php on line 36
where did go wrong?
demo.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 , __set</h2>'; unset($address->postal_code); echo $address->display(); echo '<h2>testing address __construct 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 address protected $_address_id; // when recorded created , 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 address can populated. if(!is_array($data)){ trigger_error('unable construct address ' . get_class($name)); } // if there @ lease 1 value, populate address if(count($data) > 0) { foreach($data $name => $value){ // special case 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 set postal code. if ('postal_code' == $name){ $this->$name = $value; return; } // unable 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 access property; trigger error trigger_error('undefined_property via __get: ' . $name); return null; } /* guess postal code given subdivision , city name. @todo replace database lookup @return string */ protected function _postal_code_guess(){ return 'lookup'; } /* display 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>
function __get()
isn't closed. i`ve removed code make work. check differences.
<?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 address protected $_address_id; // when recorded created , 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 address can populated. if(!is_array($data)){ trigger_error('unable construct address ' . get_class($name)); } // if there @ lease 1 value, populate address if(count($data) > 0) { foreach($data $name => $value){ // special case 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 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 set postal code. if ('postal_code' == $name){ $this->$name = $value; return; } // unable access property; trigger error, trigger_error('undefined or unallowed property via __set(): ' . $name); } /* magic __tostring @return string */ function __tostring() { return $this->display(); } /* guess postal code given subdivision , city name. @todo replace database lookup @return string */ protected function _postal_code_guess(){ return 'lookup'; } /* display 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; } }
Comments
Post a Comment