1

I have a short title and message that I want to show, defining the following:

class A {
  public $name = 'Mark';
  public $entry = array(
    'title'   => 'Some title',
    'message' => 'Hi '.$name
  );

  // Constructor
  public function __construct() {}

  // Some other functions
}

This isn't working.

Can someone explain why? Should I go for separate variables instead or is there a better way? Thanks for your time.

EDIT

5
  • 5
    what do you mean isn't working?, also, as your code is written i tried it and it worked fine, setting $entry['message'] equal to 'Hi Mark' Commented Feb 28, 2011 at 18:27
  • I think you need to show us a bit more code instead of just the declaration. Commented Feb 28, 2011 at 18:38
  • Yes, you are right. The issue seems to pop up, when wrapped in a simple class - for example wrapped in class A {...}. It says: Parse error: parse error, expecting `')'' Commented Feb 28, 2011 at 18:39
  • Please show us your code, so we can know what are you talking about :-) Commented Feb 28, 2011 at 18:41
  • Its a good question which shows how important is the topic "Runtime and compile-time ops in Classes". Commented Feb 28, 2011 at 18:50

2 Answers 2

2

You are trying to pull that off in property declaration of a Class, aren't you? Property declarations happen during COMPILE-TIME and can only accept values, not operations that require RUN-TIME to happen and concatenation definitely is a run-time operation... Put that line into your constructor method instead.

class A
{
    public $name = 'Mark';
    public $entry = array("test");
    public $var1 = someFunct();    // WRONG, ITS AN OPERATION and REQUIRES RUNTIME
    public $var2 = 1 + 2; // WRONG, ITS AN OPERATION and REQUIRES RUNTIME
    public $var3 = CLASS_NAME::SOME_CONSTANT_OR_PROPERTY_HERE; // WORKS, CONSTANTS ARE DETECTED IN COMPILE-TIME
    public $var4 = $anythingWithDollarSign; // WRONG, SYNTAX ERROR, REQUIRES RUNTIME

    public function __construct() {
        $this->entry = array( 'title' => 'Some title', 'message' => 'Hi ' . $this->name );
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I know that this is possible. But I'm trying to do something else. Anyway, thanks for the help.
The initialization of the variable through the constructor. What if entry is a static array, which I want to access from another class?
0

in classes, you can only declare variables that consist of a static value, and not a dynamic value such as "name" . $name.

What you would have to do is concat in the constructor, (That's why its there):

class A
{
    public $name = 'Mark';

    public $entry = array(
        'title'   => 'Some title',
        'message' => 'Hi %s'
    );

    // Constructor
    public function __construct()
    {
        $this->entry["message"] = sprintf($this->entry["message"],$this->name);
    }
}

2 Comments

I would call this an "overkill" if not a "dirty-code" :) No offense.
No offence taken, I only used sprintf to show the string manipulation in the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.