> -----Original Message-----
> From: ?? [mailto:xiaohan2...@gmail.com] 
> Sent: Thursday, November 25, 2010 12:21 AM
> To: php-general@lists.php.net
> Subject: [PHP]any way to iterate the fields in a class
> 
> Actually, what I am seeking is how to assign values to the 
> fields in a class
> via an array.
> I have tried like this. However failed.
> I have a class.
> *class book{
> var name;*
> *var price;*
> *}*
> *
> *
> And I have got an array.
> 
> *$array=array('name'=>'harry potter','price'=>'$122');*
> 
> By using function *extract(), *I assign the values to $name 
> and $price while
> not $this->name and $this->price, which is not what I want.
> 
> So I am thinking iterating the fields in an array and assign 
> the values each
> by each through an array.
> 
> Is there any way to assign values to the fields in a class 
> via an array.
> 
> Thanks in advance!
> 


        /**
        * generate a key/value pair from the class' variables.
        *
        * @access       public
        * @return       array
        * @author       Daevid Vincent [daevid.vinc...@panasonic.aero]
        * @version 1.0
        * @date         01/27/09
        */
        public function get_array()
        {
                $row = array();
                foreach($this as $key => $value)
                        $row[$key] = $value;

                return $row;
        }


        /**
        * Shows all exposed variables in this class
        *
        * @access       public
        * @return       array
        * @param        boolean $print to print out each value
        * @author       Daevid Vincent [daevid.vinc...@panasonic.aero]
        * @version 1.1
        * @date         01/27/09
        */
        public function iterate_visible($print = false)
        {
            if ($print) echo
"\n<BR><B>".$this->get_class_name()."::iterateVisible:</B><BR>\n";

                $tmp = array();
            foreach($this as $key => $value)
                {
                        $tmp[$key] = $value;
                        if ($print) print $key." => ".$value."<BR>\n";
                }

                return $tmp;
        }


And related:

        /**
        * Provides generic getters and setters
        *
        * @access       public
        * @param   string $method The method name.
        * @param   array $arguments The arguments passed to the method.
        * @return       mixed
        * @author       Daevid Vincent [daevid.vinc...@panasonic.aero]
        * @date         02/02/09
        * @see          __get(), __set()
        */
        public function __call( $method, $arguments )
        {
                $prefix = strtolower( substr( $method, 0, 3 ) );
                $property = strtolower( substr( $method, 4 ) );

                if ( empty($prefix) || empty($property) ) return;
                //exit("__call($method) :: prefix='$prefix' and
property='$property'");

                if ( 'get' == $prefix )
                {
                        if ( property_exists($this, $property) )
                                return $this->$property;
                        else
                                return $this->__get($property);
                }
                elseif ( 'set' == $prefix )
                {
                        if ( property_exists($this, $property) )
                                return $this->$property = $arguments[0];
                        else
                                return $this->__set($property,
$arguments[0]);
                }

                // Technically we should never get to this point as most
calls are get_() or set_()
            echo "<p><font color='#ff0000'>Attempted to
'".$prefix."->".$method."()' in class
'".$this->get_class_name()."'.</font><p>\n";
                backtrace();
        }

        /**
        * magic function to handle any accessing of undefined variables.
        * Since PHP is "lax" this will help prevent stupid mistakes.
        *
        * @access       public
        * @return       void
        * @param        mixed $property name of the variable
        * @author       Daevid Vincent [daevid.vinc...@panasonic.aero]
        * @date         2010-08-12
        * @see          __set(), __call()
        */
        public function __get($property)
        {
                if ($_SESSION['DEVELOPMENT'] && !$_SESSION['mobile'])
                {
                    echo "<p><font color='#ff0000'>Attempted to __get()
non-existant property/variable '".$property."' in class
'".$this->get_class_name()."'.</font><p>\n";
                        $this->suggest_alternative($property);
                    backtrace();
                        exit;
                }
                //else exit("__get($property) NO SUCH PROPERTY IN
".$this->get_class_name().' CLASS');
                //[dv] commented this out as it was obviously exiting on
PROD, when really it should just ignore

                //Throw new BadProperty($this, 'get', $property);
        }

        /**
        * magic function to handle any setting of undefined variables.
        * Since PHP is "lax" this will help prevent stupid mistakes.
        *
        * @access       public
        * @return       void
        * @param        mixed $property name of the variable
        * @param        mixed $val value of the variable
        * @author       Daevid Vincent [daevid.vinc...@panasonic.aero]
        * @date         2010-08-12
        * @see          __get(), __call()
        */
        public function __set($property, $val)
        {
                if ($_SESSION['DEVELOPMENT'] && !$_SESSION['mobile'])
                {
                        echo "<p><font color='#ff0000'>Attempted to __set()
non-existant property/variable '".$property."' to '".$val."' in class
'".$this->get_class_name()."'.</font><p>\n";
                        $this->suggest_alternative($property);
                        backtrace();
                        exit;
                }
                //else exit("__set($property) NO SUCH PROPERTY IN
".$this->get_class_name().' CLASS');
                //[dv] commented this out as it was obviously exiting on
PROD, when really it should just ignore

                //Throw new BadProperty($this, 'set', $property);
        }

        /**
         * Suggests alternative properties should a __get() or __set() fail
         *
         * @param       string $property
         * @return string
         * @author Daevid Vincent [daevid.vinc...@panasonic.aero]
         * @date    05/19/09
         * @see         __get(), __set(), __call()
         */
        public function suggest_alternative($property)
        {
                $parts = explode('_',$property);
                foreach($parts as $i => $p) if ( in_array($p,
array('_','id','get','set')) ) unset($parts[$i]);

                echo 'checking for <b>'.implode(' or ',$parts)."</b> and
suggesting the following:<br/>\n";

            foreach($this as $key => $value)
                        foreach($parts as $p)
                                if (stripos($key, $p) !== false)
                                {
                                        if ($property == 'get_'.$p ||
$property == 'set_'.$p)
                                                $possibilities[] =
'<li>'.$key."<b>()</b> <i>note the parenthesis</i></li>";
                                        else
                                                $possibilities[] =
'<li>'.$key."</li>";
                                }

                echo
"<ul>\n".implode("\n",array_unique($possibilities))."</ul>\n";

                //[dv] unfortunately there doesn't seem to be a way to know
if someone called this as a method () or directly :(
                //if ( strpos($property, '()') === false )
                if (count($parts) == 1 && $property == 'get_'.$parts[0] ||
$property == 'set_'.$parts[0])
                        echo 'Perhaps you forgot the parenthesis at the end
of the method call, and meant this instead: <b>'.$property.'()</b>';
        }


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to