-1

Is it possible to define a php array constant in a class with a function?

edit: "php array variable" not constant

I've tried:

assume, array func(string param1, string param2...);

class A{

    public static $const;

    public function blah(){
        self::$const = func('a','b','c',...);
    }
}

the debugger in sublime shows no value for $const after a break point at the self::$const line

marvelownz
  • 121
  • 11
  • 1
    _'Define a constant with a function'_ - isn't that an oxymoron? –  Feb 10 '15 at 21:47
  • 1
    @HoboSapiens it's not. For something being constant means it's immutable, and not necessary it is defined in compile time. – zerkms Feb 10 '15 at 21:53
  • See http://stackoverflow.com/questions/1290318/php-constants-containing-arrays – Charlie Schliesser Feb 10 '15 at 21:53
  • @CharlieS. That doesnt seem to work inside a class. – marvelownz Feb 10 '15 at 21:58
  • @HoboSapiens. I meant use a function to define a constant or a variable – marvelownz Feb 10 '15 at 21:58
  • it claims to have a syntax error when i go public static $var = func(a,b,c); it says unexpected '(' – marvelownz Feb 10 '15 at 22:05
  • @Charlie S: what is it then? Isn't it how you invoke functions? – zerkms Feb 10 '15 at 22:08
  • @CharlieS what language? `func('a','b','c',...)` -- is how you invoke a function in php (if only those `...` replaced with something meaningful) – zerkms Feb 10 '15 at 22:10
  • @CharlieS OP even provided its signature in the question `array func(string param1, string param2...);` – zerkms Feb 10 '15 at 22:13
  • func is some predefined function that also updates a sql database – marvelownz Feb 10 '15 at 22:14
  • I deleted my comments since that was really a conversation to sort out my understanding of your intent. I think your syntax issue is simply that you can't assign a function to be the value of a static var while instantiating the static var like that. But the answer I posted below and the cross-linked question I think should point you in the right direction, any other syntax issues aside. – Charlie Schliesser Feb 10 '15 at 22:22

1 Answers1

0

In PHP < 5.6 you can't have arrays as constants.

If you're looking to return a constant via a function, you'd simply do:

define('FOO', 'bar')

function getFoo() {
  return FOO;
}

but that's not returning an array and it's quite a silly example.

I think what you're really looking to do is return an array from a class's static method, something like:

class Foo {
  public static function GetFoo() {
    return array(1, 2, 3);
  }
}

Foo::GetFoo();

or if you want to run the function as a method of an object instance, you wouldn't set a static property (that doesn't make sense to me).

class Foo {
  private $foo = array();

  public function getFoo($arg1, $arg2) { // not sure what your arguments are for if this is intended to be a "constant"...
    $this->foo = array(...)
    return $this->foo
  }
}

$someFoo = new Foo();
$someFoo->getFoo(1, 2);

Does this help at all? The numerous examples at PHP Constants Containing Arrays? should help, too.

Community
  • 1
  • 1
Charlie Schliesser
  • 7,851
  • 4
  • 46
  • 76
  • if i were to use the array from your second method in another public static function, would i access it as $this->GetFoo[index] ? – marvelownz Feb 10 '15 at 22:13
  • Well it just seems like in that case it's really a singleton and not a constant, so wouldn't you want to define your singleton and just keep it around for whatever methods need it? – Charlie Schliesser Feb 10 '15 at 22:19