Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-22-2012, 08:11 AM   PM User | #1
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 792
Thanks: 210
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
Question Cannot concatinate a variable to a protected variable?

Hi

why does the following syntax generate an error:



Code:
<?php
class Foo
{
    protected $my_static = $var .  'foo';
}
Parse error: syntax error, unexpected T_VARIABLE in /var/www/test.php on line 4
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 01-22-2012, 11:20 AM   PM User | #2
bacterozoid
Regular Coder

 
bacterozoid's Avatar
 
Join Date: Jun 2002
Location: USA
Posts: 486
Thanks: 23
Thanked 35 Times in 35 Posts
bacterozoid is an unknown quantity at this point
It's not because you can't concatenate stuff, it's because you can't use a variable there. When you instantiate that class, it has no idea what $var even is.

Something like this is probably what you want:

Code:
class Foo {

  protected $foo;

  function __construct($var) {
    $this -> foo = $var . 'bar';
  }

}
bacterozoid is offline   Reply With Quote
Users who have thanked bacterozoid for this post:
cancer10 (01-22-2012)
Old 01-22-2012, 02:55 PM   PM User | #3
cancer10
Regular Coder

 
Join Date: Jun 2006
Location: India
Posts: 792
Thanks: 210
Thanked 2 Times in 2 Posts
cancer10 can only hope to improve
That solved my problem.

Many thanks sir.
__________________
http://outlineme.com/cancer10
cancer10 is offline   Reply With Quote
Old 01-22-2012, 02:59 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,647
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This description is a little inaccurate, though on the right lines. It doesn't matter if the scope was to include another member, the problem is that a default value to a member property cannot be an expression at all. Only constant data is allowed, not even a private $var = 4 + 4; is allowed.
As pointed out, a simple constructor gets around this problem completely. You don't even need to accept anything:
PHP Code:
public function __construct()
{
    
$this->foo 4;

This is why I recommend initializing all variables within the constructor instead of the member signature. Since I like to keep it consistent, I won't split the two up, but more often than not a member property is dynamic in nature.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:21 PM.


Advertisement
Log in to turn off these ads.