Offtopic: Inheritance of static members in PHP

thread: 4 messages  |  last: about 7 years ago  |  started: saturday, september 15, 2007, 8:35 pm pdt


#1  |  alex94040 (Seattle, WA) United States of America Qcodo Core Contributor
Saturday, September 15, 2007, 8:35 PM PDT

I'm super-sorry for the slightly offtopic question; this is a super-thriving community of PHP5 OOP pros, so I figured I'd try my luck here.

I consider myself very comfortable with OOP; I've done it in PHP5 for a while, but this issue just knocks me dead.

I don't know how to use overloading with static class members. PHP5 just seems to ignore it.

class Message {   
   public static function test() {     
      return self::getMyConstant();  
   }    

   public static function getMyConstant() {   
      return "0";  
   }
}

class IncomingMessage extends Message {   
   public static function getMyConstant() {return "a";}
}

class OutgoingMessage extends Message {   
   public static function getMyConstant() {return "b";}
}

I'd like to call OutgoingMessage::getMessageType(), and receive a valid value (“b”)... Instead, I receive a “0” - wrong method gets called.

Any ideas? Is there another way of doing this statically? I really don't want to move this method/property into a the object properties since I need to call it form static context.

Thanks a bunch, and I again apologize for an offtopic.

P.S. If you want to know what I'm doing - I'm working on a daemon-based infrastructure to send out email messages asynchronously. QEmailServer is great, but it sometimes takes up to 10 seconds to send a message (using GMail), so I don't want to put that logic onto the UI thread :-)

#2  |  fecool (Bielefeld, NRW) Germany
Sunday, September 16, 2007, 5:22 AM PDT

You are completely right: PHP ignores this overloading. The real problem is self: It always ponints to the active Class.
Look at the PHP-Discussion for further Ideas:
http://www.php.net/manual/en/language.oop5.static.php

A good idea is how QForms are organized:

public static function Run($strClassName) {
  $this->objInstance = new $strClassName();
}

In a similar problem (I tried to write a somewhat modular Authentication-Framework) i helped me this way:
AuthUser.class.php:

class AuthUser {
  protected static $sessionVar = 'AuthUser';
  public static function setSessionVar($strNewSessionVar) {
     self:$sessionVar = $strNewSessionVar;
  }
}

CenterAuthUser.class.php:

class CenterAuthUser {
  ...
}
CenterAuthUser::setSessionVar('CenterAuthUser');

Hope this helps.

#3  |  alex94040 (Seattle, WA) United States of America Qcodo Core Contributor
Sunday, September 16, 2007, 10:09 AM PDT

fecool, thank you so much for a great advice! The QForms “Run” paradigm [passing the class name] is exactly what I ended up using. I had to make a tweak because I didn't want to create a new object of this type (i.e. I didn't want to have Factory pattern here).

Instead, I went with an approach from the PHP5 discussion page you pointed me to (THANK YOU SO MUCH, btw). Here's the code:

class Message {
   public static $myVar = "0";
    
   public static function test($class) {
         $vars = get_class_vars($class);
         echo $vars['myVar'];
   }
}

class IncomingMessage extends Message {
    public static $myVar = "a";
}

class OutgoingMessage extends Message {
    public static $myVar = "b";
}

echo OutgoingMessage::test("OutgoingMessage");
.bc
#4  |  VexedPanda (Calgary, AB) Canada
Wednesday, September 19, 2007, 8:09 AM PDT

Myself, I would have done:

<?
class Message {
    public static $myVar = "0";
    
    public static function testClass($class) {
        $vars = get_class_vars($class);
        echo $vars['myVar'];
    }

    public static function test() {
        return self::testClass("Message");
    }
}

class IncomingMessage extends Message {
    public static $myVar = "a";
    public static function test() {
        return parent::testClass("IncomingMessage");
    }
}

class OutgoingMessage extends Message {
    public static $myVar = "b";
    public static function test() {
        return parent::testClass("OutgoingMessage");
    }
}

echo OutgoingMessage::test();
?>
.bc


Copyright © 2005 - 2014, Quasidea Development, LLC
This open-source framework for PHP is released under the terms of The MIT License.