2

I've read that it's better to have properties of a class be private, and use get/set methods to access/change them.

So I set up a class that way, but I want to be able to display the properties in an html table and I was going to use a separate htmlTable display class to do it.

I thought of 4 possibilities. Feel free to skip them if you already know the ideal way to do this.

Thank you.

Possibilities:

  1. I can get the class fields using:

    $class_Vars = get_class_vars($object_class);
    $fields = $class_vars['fields'];
    
    // But as far as iterating through each object, this doesn't work:
    
    foreach($object_array as $current_object) {
        foreach($current_object as $value) {
            $html = '<td>' . $value . '</td>';
        }
    }
    

    The values are private and inaccessible.

  2. A possible solution that looks very clumsy and would probably be a debugging nightmare is:

    foreach($fields as $value) {
        $get_func = 'get' . ucwords($value); // e.g. $get_func = 'getId'
        $current_value = $current_object->$get_func();
    }
    

    I think it would work but it doesn't sit right with me.

  3. Interface. Another possibility is to write in an htmlTable function into every class I want to do it. But that is a lot of code reuse.

  4. Interface. Or I could write in an export() function into every class that just outputs an array with property names and values. Then my htmlTable class can just handle those outputs.

4
  • PHP's reflection might help. php.net/manual/en/reflection.extending.php Commented Apr 19, 2012 at 5:17
  • actually i would say that it is better to set variables as protected, then the classes, which inherit from the original, can access them too. Commented Apr 19, 2012 at 5:22
  • @tereško That depends on whether the classes that inherit should have access. Be wary of inheritance - it's one of the most over-used OOP features. But if you do plan for inheritance make sure your original class is "Open to extension but not to modification". en.wikipedia.org/wiki/Open/closed_principle Commented Apr 19, 2012 at 6:23
  • @liquorvicar , i wouldn't have known .. =P Commented Apr 19, 2012 at 6:46

1 Answer 1

1

Either make them public or make get/set methods.

You could also make a getAll() method that returns an associative array with all of your private variables.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem with get/set methods is coding for the function names, which will be different for each property. I just tried the getAll() method (which I mentioned in my question as "export()") and it does work. Also, annoyingly, in Dreamweaver this is flagged as an error even though it's not: "$fields = $object_class_name::$fields"
You could just write the __get($name) { return $this->$name; } magic method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.