/    Sign up×
Community /Pin to ProfileBookmark

Parse error in a database class

[code=php] protected function checkLocation() //check if we’re on my computer or the server
{
if (str_replace(‘xampp’, ”, $_SERVER[‘SCRIPT_FILENAME’]) != $_SERVER[‘SCRIPT_FILENAME’])
{
return ‘host111.hostmonster.com’;
}
else
{
return ‘localhost’;
}
}

private $db_host = $this->checkLocation(); // Database Host
private $db_user = ‘myusername’; // Username
private $db_pass = ‘mypass’; // Password
private $db_name = ‘mydbname’; // Database
[/code]

I’m getting this error: [I]PHP Parse error: syntax error, unexpected T_VARIABLE in /home2/myuniverse/public_html/site/alpha/Dogs.class.php on line 27[/I]

Line 27 is:

[code=php]private $db_host = $this->checkLocation(); // Database Host[/code]

For testing purposes, I did the following to the checkLocation() function, and it did not help:

[code=php]protected function checkLocation()
{

return ‘localhost’;
}[/code]

For testing purposes I also did the following, and the parse error DID go away:

[code=php]private $db_host = ‘localhost’; // Database Host[/code]

So, what I want to know is, how can I get the parse error to go away, but still get to use my checkLocation() function to determine the path to the database?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 29.2012 — You cannot call a function/method to assign a default value.

http://www.php.net/manual/en/language.oop5.properties.php
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated. [/quote]

You can, however, move the initialization to the __construct() method, so that the property gets initialized before any other methods are called.
Copy linkTweet thisAlerts:
@evenstar7139authorMar 29.2012 — You cannot call a function/method to assign a default value.[/QUOTE]

Forgive my ignorance, but what do you mean, in this context, by default value?

You can, however, move the initialization to the __construct() method, so that the property gets initialized before any other methods are called.[/QUOTE]

So you're saying, I should change my checkLocation() function into a constructor?
Copy linkTweet thisAlerts:
@NogDogMar 30.2012 — This is "legal":
[code=php]
class Foo
{
private $bar = 'some value';
}
[/code]

This is not:
[code=php]
class Foo
{
private $bar = any_function_you_can_think_of();
}
[/code]

I typically work around it this way:
[code=php]
class Foo
{
private $bar;

public function __construct()
{
$this->bar = $this->some_function();
}

private function some_function()
{
return 'some value';
}
}
[/code]
×

Success!

Help @evenstar7139 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...