2

Possible Duplicate:
how do i access static member of a class?

I have this class. In its createNew function I refer to $reftbArr = tc_group::$tblFields;.

I have many classes similar to this class a couple of times. They have the same methods and variables but of course the class name is different.

How is the best way in the function createNew to access tc_group::$tblFields; but not have the hardcoded class name?

<?php
class tc_group {
    public $id;
    public $password;
    private static $tableName = "tc_group";

    public static $tblFields = array(
        ':id'        => array('value' => '','required' => 0),
        ':password'  => array('value' => '','required' => 0)
    );

   public static function createNew($link , $tblfields){
    $reftbArr = tc_group::$tblFields;
   }
}
?>
Community
  • 1
  • 1
randy
  • 1,685
  • 3
  • 34
  • 74

3 Answers3

2

You can use self for example: self::$variable

You can use static for example: static::$variable

The difference is that self will access the current class it is used in (ignoring inheritance), static will obey inheritance.

Extended Answer

I have this same class a couple of times. they have the same methods and vars but of course the class name is different.

That sounds like it needs refactoring. In which case create an "abstract class" which defines all the properties and methods defined by the other classes and remove them from the other classes (as they are now in the abstract class). Then extend the abstract class to create the separate classes as needed that contain ONLY WHAT CHANGES in them.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
HenchHacker
  • 1,616
  • 1
  • 10
  • 16
1

You would use the static keyword self.

public static function createNew($link , $tblfields){
    $reftbArr = self::$tblFields;
}
nickb
  • 59,313
  • 13
  • 108
  • 143
1

$tableFields = self::$tblFields;

print_r($tableFields);

Johndave Decano
  • 2,101
  • 2
  • 16
  • 16