0

what will be output of the following program

<?php
class Test
{
    protected $_value;

    public function __construct()
    {
        $this->_value = 1;
    }

    public static function getOutput()
    {
        //echo "hello";
        return $this->_value*5;
    }
}
echo Test::getOutput();
?>


I think it should be 5 but it gives blank.Can anybody describe why it blank page?
1
  • 4
    I'm voting to close this question as off-topic because this not related to magento and should be asked on stackoverflow.com.
    – sv3n
    Jul 17, 2017 at 13:59

1 Answer 1

2

You are using $this in a static method. This will not work, because $this can only be used in an object context.

This example will get you the result you expect

<?php

class Test
{
    protected static $_value = 1;

    public static function getOutput()
    {
        return static::$_value * 5;
    }
}

echo Test::getOutput();

You can find more information on how static methods work in the PHP manual: http://php.net/manual/en/language.oop5.static.php

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.