1

I want the variable to be parsed within the array so when I echo $head['meta_title'] , lol is displayed. I have tried wrapping it in double quotes but that doesn't seem to work either, is there any way round this?? Thanks!

I am getting either unexpected T_VARIABLE and when I use double quotes I get unexpected ""

$meta_title = "lol";

public $head = array
(
    "title"        => "blah",
    "meta_title"   => $meta_title,
    "meta_content" => $meta_content
);
8
  • Why have you put a comma after $meta_content? Remove it Commented Oct 23, 2012 at 12:40
  • remove the comma at the end of $meta_content, Commented Oct 23, 2012 at 12:40
  • @MuthuKumaran The last comma isn't doing any harm. Commented Oct 23, 2012 at 12:44
  • PHP.net includes the comma in their array examples, so I assumed it was good practice Commented Oct 23, 2012 at 12:47
  • The comma, although it isn't harmful, does encourage a bad habit of using them everywhere. In a SQL select list, for example, it would be a syntax error. Worse, it is fine in JavaScript arrays for most browsers but will fail in IE<=8 causing ambiguous errors and hard to track down bugs. Best not to get in the habit of using trailing commas. Commented Oct 23, 2012 at 12:49

3 Answers 3

5

You cannot use an expression to initialize a class property. The values of the two variables are not known until runtime, and therefore can't be used in the declaration. Instead, define them in the constructor.

public $head = array
(
    // The title as a string literal is ok...
    "title"        => "blah",
    "meta_title"   => NULL,
    "meta_content" => NULL
);
// Pass them to the constructor as parameters
public function __construct($meta_title, $meta_content)
{ 
  // Initialize them in the constructor. 
  $this->head['meta_title'] = $meta_title;
  $this->head['meta_content'] = $meta_content;
}

From the docs

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.

8 Comments

Ah ok, that would make sense, I actually have them defined in another class, class standardHandle { //-- <head> Variables --// //Metadata public $meta_title = "lol"; public $meta_content = "mao"; //Stylesheet public $style = array ( "link" => "style.css", "type" => "screen", ); }
Ok, do I add them to the contructor of their native class?
@user1650757 Pass them into the constructor of the class that needs to use them. If they were defined in another class, that would be something like: new ThisClass($otherclass->meta_title, $otherclass->meta_content);
hm is there any other alternative, what if I had hundreds of variables from the other class that needed passing...apologies if im not making sense, quite sleep deprived right now edit: if i was to wrap them in their native class constructor..would that work?
@user1650757 you can pass them as an array into the constructor and unpack them inside. However, if you have many variables that need to be passed between classes, it possibly suggests a design problem, where one class ought to be inheriting from the other, or instantiated inside the other, for example...
|
2

If this is within an object you cannot assign a variable this way. You have to set it using the __construct-method.

Comments

0

The public needs to be removed before $head. I've created an example in PHP Sandbox.

http://sandbox.onlinephpfunctions.com/code/d17bc90e1291f6a3b23537984df755e40446add6

2 Comments

I would say the public shows more, that he is trying to do this within the scope of an object even if the OP didn't post the whole thing.
This is a public class property declaration. php.net/manual/en/language.oop5.properties.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.