/    Sign up×
Community /Pin to ProfileBookmark

using define() inside a class constructor

I have a class [I]myclass[/I].

I would like for this class to have constants that are defined that could be accessed like: myclass->CONSTANT1

I would like the class constructor to define this constants, as it will be the constructor generating the values. Such as (pseudo):

[code=php] class testclass {

FUNCTION testclass()
{
define(‘CONSTANT1’, 1);
}
}

$myclass = new testclass();

echo ‘–>’. $myclass->CONSTANT1; //THIS VALUE IS NOT PRINTING![/code]

How can I get this to work?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chazzyOct 31.2006 — because it doesn't work that way. you need to give your class members, and then assign values to them. constants inside of a class are typically referenced as "static" members.
Copy linkTweet thisAlerts:
@ilbonparaurtiauthorOct 31.2006 — I was under the impression that the value of static members could be changed. I am looking for a way to group unchangable variables from within a class.
Copy linkTweet thisAlerts:
@ilbonparaurtiauthorOct 31.2006 — Using PHP 4.3
×

Success!

Help @ilbonparaurti spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...