-1

Is it anyhow possible within a PHP class to reference from one static variable to another?

class EmployeeDAO
{
    private static $FIND_ALL = 'SELECT * FROM employee';

    private static $FIND_BY_NAME = self::$FIND_ALL . // This is not allowed!
        ' WHERE employee.name LIKE :name';
}
LowLevel
  • 1,085
  • 1
  • 13
  • 34

3 Answers3

1

Starting form php 5.6 you can do like this:

class EmployeeDAO
{
    const FIND_ALL = 'SELECT * FROM employee';

    const FIND_BY_NAME = self::FIND_ALL .
        ' WHERE employee.name LIKE :name';
}
LowLevel
  • 1,085
  • 1
  • 13
  • 34
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
1

A property declared as static cannot be accessed with an instantiated class object (though a static method can). For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.

PHP: Static Keyword - Manual

FKEinternet
  • 1,050
  • 1
  • 11
  • 20
  • And what if I use private static functions to return the appropriate strings and to reference to each other? – LowLevel Apr 13 '17 at 23:07
0

I'm not sure this is an appreciated way, but I could solve it this way:

private static function sql_findAll()
{
    return 'SELECT * FROM employee';
}

private static function sql_findByName()
{
    return self::sql_findAll() .
        ' WHERE employee.name LIKE :name';
}
LowLevel
  • 1,085
  • 1
  • 13
  • 34