Jump to content

LostInTheWoods

New Members
  • Posts

    7
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

LostInTheWoods's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks Guys! I normally do use echo, and I now have a good reason to continue doing so more consistently. I don't understand your point keeB. You have placed the property values into an array instead of using them directly. What is the advantage of this? Thanks, Stephen
  2. Hi, This is weird, I have tried your example code and it works fine. The complete scenario of what I am trying to do is of course more complicated than I gave in my first example, but I can't see why this weird behaviour is happening. Here is the bigger picture: I am retrieving an array of result objects from a query and I am trying to write a function that will retrieve the properties of all the objects. So I am looping over the array and then using the properties from each result object. In an attempt to make the function more generic, I thought passing in a property name would be useful. I do a check before trying to retrieve a property to see that it exists, and it does: if(property_exists($result, $value) && property_exists($result, $display)){ print("<option value=\"$result->$value\" >$result->$display</option>"); } but this outputs '->$display' in the select options. And I also get the PHP error: "Object of class stdClass could not be converted to string" What is the obvious thing I am missing here? Thanks, Stephen
  3. Hi, I need a little help with retrieving properties from an object, but having the name of the property passed in to a function. Er code might help explain... Here 'version'is the name of the object property I am trying to retrieve: // Property name hard coded in function function myFunction1($object){ print($object->version); } // Property passed in as a parameter. function myFunction2($object, $propertyName){ print($object->$propertyName); // Wrong! print($object->[$propertyName]); // Wrong! print($object->[propertyName]); // Wrong! } So how can I retrieve an object's property by it's name, but dynamically? Many Thanks, Stephen
  4. Well, thanks for that. First question on this forum and I get a example solution! Thanks again, LITW
  5. Now you said that a penny dropped. I changed the private to protected and it works. Which kind of makes sense.... I think. So the method is being 'run' from the abstract class so it needs to have access to the properties... so protected works (as does of course public does too).
  6. The properties are private. But my thought was that the method is being inherited, and it can be used as if it was in the object class. I use this method mainly for debugging, and my thought was I could just go to the abstract class at the end of development and remove it, rather than hunting through my objects.
  7. Hi, First post, so be nice All my objects have getters and setters, and a method called getAllProperties() that loops through the objects properties and returns them in an array, like so: public function getAllProperties() { $all = array(); foreach($this as $var => $value) { $all[$var] = $value; } return $all; } But to avoid lots of repetition I thought I would put this method in an abstract class so all my objects that extend the abstract class can use the method. But no. For some reason it is not possible to loop over the properties of the (non abstract) object. Other stuff works, like calling a getter, but no the looping. Any ideas? Many Thanks, LITW
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.