Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define constant in class constructor?

Can I define a class constant inside the class constructor function ?

(based on certain conditions)

like image 241
EEk Avatar asked Aug 22 '11 09:08

EEk


2 Answers

That goes against the idea of class constants - they should not be dependent on a specific instance. You should use a variable instead.

However, if you insist on doing this, are very adventurous and can install PHP extensions, you can have a look at the runkit extension that allows to modify classes and their constants at runtime. See this doc: http://www.php.net/manual/en/function.runkit-constant-add.php

like image 199
chiborg Avatar answered Sep 27 '22 22:09

chiborg


I don't think you can.

It wouldn't make sense, either - a class constant can be used in a static context, where there is no constructor in the first place.

You'll have to use a variable instead - that's what they're there for.

like image 41
Pekka Avatar answered Sep 27 '22 22:09

Pekka