/    Sign up×
Community /Pin to ProfileBookmark

Class with $_GET as variable doesn’t work?

Hello,
I can’t figure out why this doesn’t work. Because aren’t both ways exactly the same?

[code=php]
<?php

class checkBlog
{
var $id;

function __construct()
{
$this->id=$_GET[‘b’];
}

}

$checkBlog = new checkBlog();
?>
[/code]

[code=php]
<?php

class checkBlog
{
var $id = $_GET[‘b’];

function __construct()
{

}

}

$checkBlog = new checkBlog();
?>
[/code]

but with the second method:

[CODE]Parse error: syntax error, unexpected T_VARIABLE in[/CODE]

Thanks in common,
David

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 16.2012 — Class variables can only be initialized with literal values, not variables nor other expressions. It's a syntax error, but essentially it makes a certain sense, since when it's part of the class variable definition, you'd be trying to make the assignment at compile time (which is before $_GET actually gets populated), whereas when you assign it via the constructor it is assigned at run time, at which point $_GET has been populated. (An alternative would be to pass it as an argument to the constructor, so that it would be less closely coupled with the source of that value and theoretically more re-usable.)
Copy linkTweet thisAlerts:
@DavidBontingauthorOct 16.2012 — Oké thanks!
×

Success!

Help @DavidBonting 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.19,
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,
)...