I've got a question about concatenating a property within a class.

Here's sample code

define('ACTION', 'say something');
class Something {
	var $text = ACTION . " else";

function prnt() {
	echo ACTION;
}

}

Doing this produces the error.

Parse error: syntax error, unexpected '.', expecting ',' or ';' in C:\apache\htdocs\test.php on line 6

Is there a reason for this?

Additionally i'm unable to define a constant in a class.

Here's another code example.

class Something {
	define('ACTION', 'say something');
        ....

}

This example produces this error message.

Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\apache\htdocs\test.php on line 6

Taken from the php manual.

Constants may be defined and accessed anywhere without regard to variable scoping rules;

http://www.php.net/manual/en/language.constants.php

So am I wrong to think that one should be able to define a constant within the class body.

    Yes that is correct, tho you cant define a constant outside of the function.

    and

    function prnt() 
    { 
        echo ACTION; 
    }
    

    should be

    function prnt() 
    { 
        return ACTION; 
    }
    

    Unless I'm mistaken. Hope I helped

      You will need to set the value in your constructor:

      define('ACTION', 'say something');
      
      class Something 
      {
          var $text = '';
      
         function Something()  // or "function __construct()" if using PHP5
         {
            $this->text = ACTION . " else";
         }
      
         function prnt() {
            echo ACTION;
         }
      }
      

        Thanks for the responses. I came to same conclusion but was under the impression that what I had done was valid.

        Edit: @ the line you eluded wasn't causing the error either echo or return works. I was using echo because I wanted the constant to be displayed when I called that method.

          Write a Reply...