Iterated calling static variables from different classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • juro
    New Member
    • Jul 2007
    • 3

    Iterated calling static variables from different classes

    Hi,
    I have a small problem:
    I'd like to call static variables from different classes - their names are stored in an array. Example of a class:
    [code=php]class MyClass1{
    public static $mysql_table = "table1";
    }[/code]

    Now I would like to do something like this:
    [code=php]$classes=array( "MyClass1","MyC lass2","MyClass 3");
    foreach ($classes as $item)
    {
    $class_table = $item::$mysql_t able; // buggy line
    // more code here: accessing database
    }[/code]

    I know this won't work. The only solution I was able to come up with was eval():

    [code=php]
    eval("\$class_t able = ".$item."::\$my sql_table;");
    [/code]

    But eval() is slow and insecure. Does anybody know any better solution?

    Thanks in advance.
    Juro
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    It looks like your design has added complexity, so that the only way out is to abuse the theory of classes.
    There must surely be a simpler solution.
    What is your aim and current design?

    Comment

    • juro
      New Member
      • Jul 2007
      • 3

      #3
      Originally posted by code green
      It looks like your design has added complexity, so that the only way out is to abuse the theory of classes.
      There must surely be a simpler solution.
      What is your aim and current design?
      I'm trying to do something following: I have more classes (10+) related to the tables in database. Those classes are extendnig base class called Object which implements global methods like Load(), Save(), Delete(). This problem is especially while using Delete() because I have to delete not only a row in one table, but also every row in other tables which depend on the deleted row.

      So i decided to create static variable $dependent_clas ses in every class, that holds names of classes which must be processed in Delete() operation.

      Sure I can also overload Delete() in every class, but this isn't so nice as one Delete(), which would be able to process any class.

      The basic question: Is there any posibility to call static variable with name of the class stored inside other variable? Or is this approach wrong?

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I have more classes (10+) related to the tables in database.
        I don't like the sound of this bit. A class for each table? What happens when new tables are needed? Surely you want a class, then instatiate an object of that class for each table. Not even this should be considered. An object for each database yes. Then each object has full functionallity of the class. If a particular table needs particular treatment this should be a seperate class method. I would have a generic class to handle basic database functionality - update, delete, insert. Then a special class that inherits the first with individual database funtionality. This may not fit in with your design but it is more like the OO programming model.

        Comment

        Working...