0

I am trying to access a function but shows error Fatal error: Using $this when not in object context

I have tried to exclude static where i am using $this but still not wotking

class Header
{
    private static $instance = null;
    public $styles = '';

    public function __construct( $styles ){
        if(!empty($styles)){
            $this->setStyles($styles);
        }
    }
    public function setStyles( $styles ){
        return $this->styles = $styles;
    }
    public function getStyles(){
        return $this->styles;
    }
    public function renderHeader(){
        return self::headerDefaults();
    }
    public static function headerDefaults(){
        self::headerLayout($this->getStyles()); //error is on this line
    }
    public static function headerLayout( $styles ){
        switch ($styles) {
            case 'one':
                self::headerStyleOne();
                break;

            default:
                break;
        }
    }
    public static function headerStyleOne(){
        ?>
            <div>Header1</div>
        <?php
    }

    public static function getInstance(){
        if (empty(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }
}

and also calling the function like

$header = new Header('one');
echo $header->renderHeader();
Tick Twitch
  • 513
  • 10
  • 23

0 Answers0