Re: [PHP] Class declaration, constants and array

2005-01-28 Thread Bret Hughes
On Fri, 2005-01-28 at 14:50, Marek wrote:
> php5 class {
> 
> const _SOMETHING_ = 'test';
> 
> private $abc=_SOMETHING_;  // fails, well actually anything fails
> similar to this.
> var $test=$test2;// also fails
> 
> So since I can not use dynamic var assignment within the class declaration,
> what are some of the easy solutions to this ? without making anything global
> to the script ?
> 
> Thanks

I believe you will have to establish stuff like this in the
constructor.  jsut create a function the same name as the class and it
will be run on object instantiation.  I had to do the a couple of years
ago for an array.

Check out the manual it used to explain this.
Bret

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Class declaration, constants and array

2005-01-28 Thread Jochem Maas
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Class declaration, constants and array

2005-01-28 Thread Richard Lynch
Marek wrote:
> php5 class {
>
> const _SOMETHING_ = 'test';
>
> private $abc=_SOMETHING_;  // fails, well actually anything fails
> similar to this.
> var $test=$test2;// also fails
>
> So since I can not use dynamic var assignment within the class
> declaration,
> what are some of the easy solutions to this ? without making anything
> global
> to the script ?

private $abc = 'test';

Now, if you NEED to use some kind of constant, you could write a PHP
script to create your class definition, and then you'd have what you
want...

That can't be too hard, since I once had a guy I was working for who
thought he had laid out a couple months' work for me, only I was done in 3
days, since it was all building classes to mirror MySQL table/fields in a
very brain-dead straight-forward fashion, and I wrote code like this, and
I don't even really grok PHP classes all that well.

Not that I think that made for a particularly good Design, but that's what
he wanted, so who am I to say?  But I sure wasn't gonna sit there typing
like a monkey when I could just script the same thing.

Or I guess you could hack something up with eval() if you really worked at
it...  That would be pretty nasty code, though, I think.

It would be nice if PHP allowed constants there, but it doesn't, so there
it is.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Class declaration, constants and array

2005-01-28 Thread Matthew Fonda
t'isnt good OOP practice to do what you want to do in the first place
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
> similar to this.
> var $test=$test2;// also fails
> 
> So since I can not use dynamic var assignment within the class declaration,
> what are some of the easy solutions to this ? without making anything global
> to the script ?
> 
> Thanks
-- 
Regards,
Matthew Fonda

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Class declaration, constants and array

2005-01-28 Thread Marek
php5 class {

const _SOMETHING_ = 'test';

private $abc=_SOMETHING_;  // fails, well actually anything fails
similar to this.
var $test=$test2;// also fails

So since I can not use dynamic var assignment within the class declaration,
what are some of the easy solutions to this ? without making anything global
to the script ?

Thanks

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php