1

Is it possible to use another static variable to initialize another static variable in php?

public static $conf_siteroot = "http://mysite.com/";
 public static $conf_docroot = "C:/Users/Mp/Documents/Projects/nana/webroot/";

 public static $conf_doclib = $conf_docroot."library/";
 public static $conf_sitelib = $conf_siteroot."library/";

This code doesn't work at all but I need to reuse the static variable so I will not write too much. Thanks

Netorica
  • 18,523
  • 17
  • 73
  • 108
  • 1
    I don't think so, but I guess you can try out - just use the proper syntax: `public static $conf_doclib = self::$conf_docroot."library/";` – Pekka Oct 11 '11 at 08:35
  • 3
    What about using constants instead of `static` variables? – Jürgen Thelen Oct 11 '11 at 08:37
  • Yeah, that should work - use self:: as Pekka says. This only won't work with class constants, but public static vars should do it fine. – halfer Oct 11 '11 at 08:38
  • You can also check this [link](http://docstore.mik.ua/orelly/webprog/pcook/ch05_06.htm) and this one [link](http://www.wellho.net/mouth/1380_Static-variables-in-PHP.html),.. Hope it helps.. – Puzo Oct 11 '11 at 08:43
  • @azat - you're right, it doesn't work. I just learnt something :-). OP, you can initialise dynamic values ($conf_doclib, $conf_sitelib) in the constructor instead. – halfer Oct 11 '11 at 10:35

3 Answers3

0

The short answer is - no

But you may reinitialize static var in _clone or _construct
But this is not exactly what you need

You could write something like this:

<?
// file Foo.php

class Foo {
    public static $prefix = 'foo';
    public static $bar;
}

// Bootstrapping
foo::$bar = foo::$prefix . '/bar';
azat
  • 3,545
  • 1
  • 28
  • 30
  • ok... any other code suggestion? I really want to make this configuration variables to be available in my functions without using global – Netorica Oct 11 '11 at 08:43
0

You can not assign values dynamically to static properties at runtime.

However you can write static getter functions that return the values you're looking for:

class Foo 
{
    public static $conf_siteroot = "http://mysite.com/";
    public static $conf_docroot = "C:/Users/Mp/Documents/Projects/nana/webroot/";

    /**
     * exemplary getter function for the ConfDocLib value
     */
    public static function getConfDocLib()
    {
        return self::$conf_docroot."library/";
    }
    ...
}

Foo::getConfDocLib(); # your dynamic value, statically accessible globally.

Or you assign them to the superglobal array $_GLOBALS which might be pretty much the same your public static properties probably. Just another idea.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • There is not way to declare static getter function http://stackoverflow.com/questions/1279382/magic-get-getter-for-static-properties-in-php – azat Oct 11 '11 at 08:59
  • @azat: You've misread my answer, I was not talking about overloading. That explicitly I've neglected, read the first sentence *"You can not assign values dynamically to static properties at runtime."* (which includes PHP's overloading magic methods). If you do not know what a getter function is, have a read [Why getter and setter methods are evil](http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html). – hakre Oct 11 '11 at 09:04
  • @azat: Added an example as well in case it's helpful. – hakre Oct 11 '11 at 09:07
  • @harke, could you post example with `$GLOBALS` ? – azat Oct 11 '11 at 09:17
  • @azat: See your own answer, the line after `// Bootstrapping` - it's basically that but just with a `$GLOBALS` prefix. It's just only an idea, not really something I want to propagate with an example because that highly depends on the specific program work-flow. – hakre Oct 11 '11 at 09:21
  • @harke, getters and setters are not evil, just need to use them to their places – azat Oct 11 '11 at 09:22
  • @harke, about globals - got it – azat Oct 11 '11 at 09:23
  • If its static, why the heck it neeeds a getter/setter method? Simply use `ClassName::$static_variable`. – daGrevis Oct 11 '11 at 09:58
  • @daGrevis: Dynamic creation of return values. Don't ask me for what that is good, anyway ;) – hakre Oct 11 '11 at 11:13
0

You can not do it this way.

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

xdazz
  • 158,678
  • 38
  • 247
  • 274