2

Alright so I think this may be extremely basic, but it has me stumped nonetheless. Before I get to my question, let me demonstrate the concept my question is based on with this working example:

<?php

$a = 'Stack';
$b = $a.' Overflow';
echo $b; // Result: "Stack Overflow"

?>

In the above example, $b is defined as the combination of $a and ' Overflow'. Now, let's assume I want to do the same thing as above except I don't want to use global variables. I want to use classes. This is how I have attempted to achieve that:

<?php

class ClassName {
    public $a = 'Stack';
    public $b = $this->a.' Overflow'; // This gives me: "Parse error: syntax error, unexpected '$this'"
}

$instantiate = new ClassName;
echo $instantiate->$b; // Desired result: "Stack Overflow"

?>

As stated, this attempt results in an error. To me, this attempt seems logical, but I guess PHP doesn't think so.

Question: Is this possible, and if so, how do I go about achieving the desired result? Also, if you could explain why my attempt has failed (logically), that would be a bonus.

I've searched and researched for hours on end trying to find an answer or figure this out on my own, but for the life of me, I cannot find anyone or anything that even touches on this (including other Stack Overflow threads). I can't even find anywhere saying it's impossible or anything of the sort either.

I'm a PHP novice, so I may need more explanation than others, but any kind of help or general guidance would be much appreciated. Thank you.

3 Answers 3

4

You cannot use $this when defining the class because it refers to a concrete object context which becomes available after instantiation. You can use a constructor for that kinds of stuff.

class ClassName 
    {
        public $a = 'Stack';
        public $b = "";
        function __construct()
        {
          $this->b = $this->a.' Overflow'; 
        }
    }

$instantiate = new ClassName;
echo $instantiate->b; 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the suggestion. I tried it, but unfortunately it still doesn't work for me... I've updated my post with the errors it gave me.
Sorry for the typo. try $instantiate->b instead of $instantiate->$b.
2

Quoting from the PHP Docs about defining class 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.

(my emphasis)

Concatenation cannot be evaluated at compile time, only at run time.


If you need to do this, then define the initial value for the property in the constructor

Comments

0

This would throw a syntax error because you are using $this in a scope that it is not allowed to:

The pseudo-variable $this is available inside any class method when that method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

This means, even if you wanted you won't be able to do what you want to do because of such restriction. This is a common restriction that is in many programming languages, properties have to be initialized to static values. To solve your issue you can do any of the following:

  • In your constructor, create those variables.
  • Create the second variable on its own, and create a method that concatenates the two.
  • Create a magic method to do it for you.

The same restriction holds true for static class properties.

Comments

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.