Access private attributes via a php loop

Asked

Viewed 32 times

0

I am making a class for my database interaction and need to assemble my DML commands based on a past class instance.

    foreach($obj as $key => $value){
        //monta comando DML 
    }

This only works if the class attributes are public, in the case of private this foreach does not give any loop. Does anyone have any idea how I can do that? Or the only way would be to oblige the passed object to have a method that returns its own attributes?

  • It gives with reflection, but without putting an example has no way of giving an opinion with certainty, and it is not always good to do this...

  • "Or is the only way to force the passed object to have a method that returns its own attributes?" -- Yes, this is called encapsulation. We usually use the attribute name prefixed by "get"

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.