Re: Class declaration, constants and array

From: Date: Sat, 29 Jan 2005 00:46:38 +0000
Subject: Re: Class declaration, constants and array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-207653@lists.php.net to get a copy of this message
Matthew Fonda wrote:
t'isnt good OOP practice to do what you want to do in the first place
Im not too hot on 'good practice' - if I understand the code and its neatly laid out then Im happy... but there is no point setting this value here if the value is to be used in an instantiated object (which it must be as its not declared 'static'). here is the declaration of a member var of 'result set' class, its maybe bad practice but its worked since php5 was in beta.
    /**
     * the IBASE_ fetch modifiers;
     *
     * @var     array
     * @access  private
     */
    private $fetchArgs = IBASE_UNIXTIME;
perhaps use the constructor to do it On Fri, 2005-01-28 at 12:50, Marek wrote:
php5 class { const _SOMETHING_ = 'test';
   private $abc=_SOMETHING_;      // fails, well actually anything fails
this wont work, for starters your referencing a non-existant constant. try: (all code is tested on php5.0.2 cli) php -r ' class MyClass { const _SOMETHING_ = "test"; public $abc = MyClass::_SOMETHING_; // .... } $a = new Myclass; echo $a->abc,"\n"; ' really you should use the constructor: php -r ' class MyClass { const _SOMETHING_ = "test"; private $abc; function __construct() { $this->abc = self::_SOMETHING_; echo $this->abc,"\n"; } } $a = new MyClass; ' although setting a private member var to the value of a constant belonging to the class of the object _seems_ silly - there may be a good reason to, alternatively you could consider referencing the class constant directly e.g. MyClass::_SOMETHING_ or if the code is inside the class MyClass: self::_SOMETHING_ alternatively you may wish to set the var as a static member variable php -r ' class MyClass { const _SOMETHING_ = "test"; private static $abc = MyClass::_SOMETHING_; // .... public static function speak() { echo self::$abc,"\n"; } } $a = new Myclass; $a->speak(); '
similar to this.
   var $test=$test2;                    // also fails
So since I can not use dynamic var assignment within the class declaration,
what does this mean?
what are some of the easy solutions to this ? without making anything global to the script ? Thanks


« previous php.general (#207653) next »