Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2005, 10:23 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default Reflecting within a class

I'm trying to write some generic code for setting the values of an object taking values from a passed-in array.

The Reflection API appeared ideal for this, and I wrote this function:
Code:
  private function _loader($array)
  {
        $rc = new ReflectionClass(get_class($this));
      $properties = $rc->getProperties();
      foreach ($properties as $property)
      {
          $key = $property->getName();
          // check for the property in the passed-in array...
            if (array_key_exists($key, $array) && !is_null($array[$key]))
          {
              echo("setting value of $key");
              // element found...
              $property->setValue($this, $array[$key]);
          }
      }
  }
however, I get an error on the $property->setValue($this, $array[$key]), being that you cannot set private/protected variables using the Reflection API. However, I am just trying to set private values from within the class - hence me passing in $this. Is there any way of doing this with the reflection API, or do I have to use eval (which I don't want to do, because it's not very elegant at all).

 
Old July 7th, 2005, 03:26 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You could try to pass $this by reference. e.g. $property->setValue(&$this, $array[$key]);

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old July 9th, 2005, 02:25 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Got what I needed using the ReflectionMethod class, but thanks very much for the reply






Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET bug: Error reflecting complex object with Nul peterwood .NET Web Services 0 February 7th, 2008 11:35 AM
Reflecting changes in Excel Worksheet...???? hunk1985 General .NET 0 September 12th, 2007 06:29 AM
Data updating in Table & reflecting in report anukagni Access 5 May 31st, 2006 03:57 AM
Reflecting a table's structure in a TreeView shakeelw Pro VB Databases 0 June 19th, 2004 06:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.