Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Static Variables in Abstract Classes

I'm working on a project where I'd like to be able to declare a static member variable inside of an abstract base class. I've got a Model class, an intermediate Post class, and finally a site-specific Post class, something like the following:

abstract class Model {
    protected static $_table    = null;
    protected static $_database = null;

    ...
}

abstract class PostModel extends Model {
    public function __construct() {
        if ( !isset(self::$_table) ) {
            self::$_table = 'some_table';
        }

        parent::__construct();
    }

    ...
}

class SitePostModel extends PostModel {
    public function __construct() {
        if ( !isset(self::$_database) ) {
            self::$_database = 'some_database';
        }

        parent::__construct();
    }

    ...
}

I'd like to make it apparent from the Model class that the $_table and $_database members are required. However, $_table is really static from the point of view of the PostModel class, and $_database is really static from the point of view of the SitePostModel class.

Is this a legitimate way to accomplish my goal? Does declaring the static variables in the Model itself imply that they should exist only once for the abstract base class, or only once for the actual instantiated class?

like image 986
michaelxor Avatar asked Oct 17 '11 18:10

michaelxor


People also ask

Can we declare static variable in abstract class?

Yes, of course you can define the static method in abstract class.

Can abstract class have static methods in PHP?

The Y() that function X() is trying to call is the parent class Y(), which is itself an abstract function. So, using abstract and static on the same method defeats each other purpose. This is the reason why PHP 5.2+ does not allow abstract static class methods.

Does PHP have static variables?

Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both. The variables and methods are accessed without the creation of an object, using the scope resolution operator(::).

Can abstract class have properties PHP?

An abstract class can have properties and methods as a regular class. But it cannot be instantiated. In most cases, an abstract class will contain at least one abstract method though it is not required. If a class contains one or more abstract methods, it must be an abstract class.


1 Answers

Is this a legitimate way to accomplish my goal?

No. It does not work, so it fails a basic test for legitimacy.

Does declaring the static variables in the Model itself imply that they should exist only once for the abstract base class, or only once for the actual instantiated class?

Static variables are global, they exist once. In your case per each classname. If you have three classnames, you would have three (global) variables. The protected keyword only controls the visibility/scope of the three static variables:

<?php

class A {
   protected static $v = 'red';
   public static function get() { return self::$v . ' - ' . static::$v;}
}

class B extends A {
   protected static $v = 'blue';
}

class C extends B {
   protected static $v = 'green';
}

echo C::get(); # red - green
like image 130
hakre Avatar answered Oct 04 '22 07:10

hakre