0

Here is my class

class Databases { public $liveresellerdb = new Database('1host1','user','pswd','db'); } 

the error i am getting is

Parse error: syntax error, unexpected T_NEW in /home/abhijitnair/sandbox/newreseller/Databases.php on line 33 

why this error is coming?

0

4 Answers 4

3

Properties may not be preset with runtime information.

Quoting PHP Manual:

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

True but you didn't suggest a solution.
@Tomalak well, the OP asked "Why is this error coming" and while I agree that pragmatically I could include a solution, I won't because of the fuzzy static class in the title.
1
<?php class Databases { public static $liveresellerdb; } Databases::$liveresellerdb = new Database('1host1','user','pswd','db'); ?> 

This is how you initialise a static member...

5 Comments

you forgot function for the ctor
I think this is a good example of how the rating system is flawed.
@Tomalak strictly speaking, the OP says static class, not static property. We could argue what is meant by that.
@Gordon: We could and I think that we should. A static class doesn't exist, so presumably we ought to work with static property: he's probably referring to the member object itself being static, misusing "class" for "object".
and not to mention the OP shows that the poster lacks the knowledge of PHP and requires to learn the basics first.
0

Because you forgot to write the static keyword to actually make the property static.

In addition, you can't initialise static properties with expressions like this. Here's a workaround.

10 Comments

I just voted down, the delete request didnt come from me. However, even if the title suggest, that the question has something to do with static classes, its simply wrong, and so your answer is.
@KingCrunch: A static class doesn't exist, so presumably we ought to work with static property: he's probably referring to the member object itself being static, misusing "class" for "object". (He certainly won't have written it for no reason at all, which seems to be your assumption.) In which case my answer is perfectly accurate, thankyou very much.
@Tomalak Hmm, @KingCrunch has a point. The OP asked "Why is this error" and the answer to that has nothing to do with the static keyword being there or not. It would still occur even if the property was declared static. The error is solely because of the what I have answered below.. So, the real cause is what you have hinted at in the second sentence, but not because the OP "forgot to put static there."
@Gordon: I know he has a point; I just happen to disagree with it. You're right about the cause: I disagree about the solution, in context.
Just a sidenote: static classes "exists" also in php. A static class is a class with only static methods and properties. So as long as you define only this kind of members (and maybe make the constructor private and the class itself final), you can call them "static class" of course, even if its not a real php language feature.
|
0

you cannot assign object during the class preperation stages, only the class instantation:

class Databases { public $liveresellerdb; public function __construct() { $this->liveresellerdb = new Database('1host1','user','pswd','db'); } } 

anything within the constructor can be generic PHP code, outside the function and instead the class body has specific laws.

if you require the database's to be static then you have to set / access them differently.

class Databases { public static $liveresellerdb; } Databases::liversellerdb = new Database('1host1','user','pswd','db'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.