Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Prosty decorator i problem z $this
Forum PHP.pl > Forum > PHP > Object-oriented programming
MMX3
Witam,

Mam dwie klasy Page i dekoruje ją Klasą Form
  1. <?php
  2. class Page{
  3.    public $pageOutput;
  4.    public function __construct() {
  5.        $this->pageOutput = $this->htmlHeader('Test');
  6.    }
  7.    /**
  8.      * Generating heading of html page
  9.      * @param $title String
  10.      * @return String
  11.      */
  12.    public function htmlHeader($title){
  13.        $heading =    ''
  14.                     .&#092;"\n\".''
  15.                    .&#092;"\n\".''
  16.                     .&#092;"\n\".''
  17.                      .&#092;"\n\t\".''
  18.                       .&#092;"\n\t\".''
  19.                       .&#092;"\n\t\".''
  20.                     .&#092;"\n\".'';
  21.    return $heading;
  22.    }
  23.    /**
  24.      * To put content all the together
  25.      * @param $content string
  26.      * @return void
  27.      */
  28.    public function htmlContent($content) {
  29.        
  30.        $this->pageOutput .= &#092;"\n\".''.\"\n\";
  31.        $this->pageOutput .= $content;
  32.        $this->pageOutput .= $this->htmlFooter();
  33.        
  34.    }
  35.    /**
  36.      * Closing Tags
  37.      * @return String
  38.      */
  39.    public function htmlFooter(){
  40.        $footer .= &#092;"\n\".''
  41.                    .&#092;"\n\".'';
  42.        return $footer;
  43.    }
  44.    
  45. }
  46. ?>

  1. <?php
  2. class Form {
  3.    public $page;
  4.    public function __construct(Page $page) {
  5.        $this->page = $page;
  6.    }
  7.    public static function htmlContent() {
  8.        
  9.        $this->page->pageOutput .= &#092;"\n\".''.\"\n\";
  10.        $this->page->pageOutput.= 'Mask
  11. Lenght
  12. ';
  13.        $this->page->pageOutput.= $this->page->htmlFooter();
  14.    }
  15. }
  16. ?>


Teraz w htmlContent udekorowanym wyrzuca mi taki błąd:
Cytat
Fatal error: Using $this when not in object context in form.php on line 8

Nie wiem z czego wynika problem i błagam o pomoc.

nospor
Cytat
Prosty decorator i głupi problem
Popraw tytuł. Glupie wyrazy nie są w nim potrzebne

Co do problemu: uzywasz $this w metodzie statycznej. metody statyczne nie posiadają $this
MMX3
Dzięki wielkie pół nocy siedziałem i nie wymotałem dzięki!
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.