1

Can somebody tell me why this code is not working? It seems like the most efficient way to do the proposed task, I don't understand why I keep getting an error - even when I reverse the Key<>Value.

I am trying to replace #tags# within a text string/array with static::variables form an external class.

ERROR:

Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in /home/content/57/10764257/html/marketing/includes/ProcessEmail.class.php on line 5

EXTERNAL CLASS:

class MyClass {
    public static $firstName = "Bob";    // Initally set as "= null", assigned
    public static $lastName = "Smith";   // statically through a call from
}                                        // another PHP file.

MAIN PHP FILE:

// This is the array of find/replace strings

private static $tags = array("#fistName#", MyClass::$firstName,
                             "#lastName#", MyClass::$lastName);

// This jumps trough the above tags, and replaces each tag with
// the static variable from MyClass.class.php

public static function processTags($message) {

    foreach ($tags as $tag => $replace) {
        $message = str_replace($tag, $replace, $message);
    }

}

But I keep getting that error...?

Thank you!

1
  • Put full code of you ProcessEmail.class.php. Then it would be simple to debug. Commented May 1, 2013 at 12:44

2 Answers 2

4

From http://php.net/manual/en/language.oop5.static.php

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

So you cannot use MyClass::$firstName as value for a static property.

Another solution is to use const instead of static. (PHP > 5.3)

class MyClass {
    const firstName = "Bob";
    const lastName = "Smith";
}

class MyClass2 {
    public static $tags = array(
        'firstName' => MyClass::firstName,
        'LastName'  => MyClass::lastName
    );
}

print_r(MyClass2::$tags);
Sign up to request clarification or add additional context in comments.

2 Comments

I think I get what you mean. So would it work if it were not static, say, I instantiated MyClass?
@Philly2NYC no that wouldn't work, too. But see my updated answer.
3

Try replacing your code for:

<?php 
class MyClass {
    private static $tags = array(
        '#firstName#' => 'Bob',
        '#lastName#'  => 'Smith'
    );

    public static function processTags(&$message) {
        foreach (self::$tags as $tag => $replace) {
            $message = str_replace($tag, $replace, $message);
        }
    }
}


$message = 'Hello, my name is #firstName# #lastName#';
MyClass::processTags($message);
echo($message);

Result:

Hello, my name is Bob Smith

3 Comments

Wait, hold up, thats not what I need - I am replacing strings within a larger string/array - not trying to get the values of my array...
No, like @redreggae mentioned, the error is thrown because you're trying to access a static variable from your class. If you want to keep things static this is the way to go, otherwise you can just remove the static keyword from $firstName and $lastName, instantiate your class and access the 2 properties with the -> operand.
Not doing what I need - in the end, just got to recode the thing... PHP rules win over common sense...

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.