Static Class Initializer in PHP

Static class initializer in PHP

Sounds like you'd be better served by a singleton rather than a bunch of static methods

class Singleton
{
/**
*
* @var Singleton
*/
private static $instance;

private function __construct()
{
// Your "heavy" initialization stuff here
}

public static function getInstance()
{
if ( is_null( self::$instance ) )
{
self::$instance = new self();
}
return self::$instance;
}

public function someMethod1()
{
// whatever
}

public function someMethod2()
{
// whatever
}
}

And then, in usage

// As opposed to this
Singleton::someMethod1();

// You'd do this
Singleton::getInstance()->someMethod1();

Static initializer or static constructor in PHP?

I don't think PHP provides any direct ways to initialize classes like it's done in Java or C#. If you want to initialize static class members you can do it within a constructor, something like this:

class MyClass {

private static $staticValue;

public function __construct() {
if (self::$staticValue === null){
self::$staticValue = 'Nice';
}
}
}

However, the above approach won't work if you never instantiate your class. That means that accessing static class members won't trigger the code in __construct unfortunately, and I don't think there's any workaround for this problem in PHP.

How to initialize static variables

PHP can't parse non-trivial expressions in initializers.

I prefer to work around this by adding code right after definition of the class:

class Foo {
static $bar;
}
Foo::$bar = array(…);

or

class Foo {
private static $bar;
static function init()
{
self::$bar = array(…);
}
}
Foo::init();

PHP 5.6 can handle some expressions now.

/* For Abstract classes */
abstract class Foo{
private static function bar(){
static $bar = null;
if ($bar == null)
bar = array(...);
return $bar;
}
/* use where necessary */
self::bar();
}

php static variable initialization

From php docs

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.

Initialize static members in PHP

From the docs,

"Like any other PHP static variable,
static properties may only be
initialized using a literal or
constant; expressions are not
allowed."

new Person() is not a literal or a constant, so this won't work.

You can use a work-around:

class School {
public static $Headmaster;
}

School::$Headmaster = new Person();

Increase static class variable in a constructor

As per PHP manual:

Static properties cannot be accessed through the object using the arrow operator ->. Like any other PHP static variable, static properties may only be initialized using a literal or constant before PHP 5.6; expressions are not allowed.

There is however a way. You must use self keyword:

class Task
{
static $tid = 0;

function __construct()
{
self::$tid++;
}
}

new Task();
new Task();

echo Task::$tid;

will output 2

Initialize static member with custom class in php

Quoting the manual page of static :

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.

That is why you cannot do

public static $NO_CACHE = new CacheMode(1, "No cache");

PHP static initializer

The error is happening because you are trying to call a private variable while not calling self to access it.

I have given the below code a test and it seems to work. Oddly, I have also given your code a test, and that seems to work fine also.

To get around this, in your compute function, you should use this return self::$lookup[0]; instead of return StaticTest::$lookup[0];.

Another way around this would be to change the private static $lookup = array(123, 456, 789); to public static $lookup = array(123, 456, 789);. Doing this, your code would work fine, but is not the recommended way.

Doing this, it should return the result you want. Think of it similar to non static classes, to access parameters (private or public) inside of a non-static class you can use $this->, where as in a static class, you should use self:: instead. Also, not that using self:: to access a variable requires the $ to be present, where as using $this-> does not.

The answer provided by Dejv would also work, but involves using a singleton type class, which is definitely not needed for this procedure as it is instancing a new class each time, completely ignoring how static variables and functions can work.

Hope this answers your question.



Related Topics



Leave a reply



Submit