0

Consider the following

Class someController {

    public static $layout = 'index';
}

Then in another scope

$layout = 'default';
$controller = 'someController';

if(property_exists($controller, 'layout')){
    $layout = $controller::$layout;
}

What will then be the content of $layout? Is it going to try to access someController::$layout or someController::default?

2
  • eval.in/302032 gives someController::$layout - although im confused as to why Commented Mar 19, 2015 at 11:19
  • @ʰᵈˑ yeah I tested it too out of curiosity, but there must be an explanation to this. Commented Mar 19, 2015 at 11:19

1 Answer 1

3

When PHP parses the code, the referencing of the static class variable takes preference over the standalone variable.

Edit: Infact, the parser does not even consider to think $controller::$layout has a reference to the $layout variable as it is using :: as @Paul Crovella mentioned in the question comments.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I just found out that to use the value of the local variable as a property name I need to use double dollar sign as in variable variables, which makes a lot of sense!
It should be noted that you can use variable variables to access a property with a variable (if that makes sense). In OPs scenario; $default = 'layout'; echo $controller::${$layout}

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.