Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Problem z przekazaniem argumentu funkcji
Forum PHP.pl > Forum > Przedszkole
tomi1985
Witam,
jestem dopiero raczkujący z php obiektowo także prosiłbym o pomoc....

a mianowicie, mam dwa pliku

config.php
Kod
class Config{
  
    public function changeSite(){
        $siteName = 'test';
        echo 'mam siteName: --'.$siteName.'--<br/>';
        return $siteName;
    }
}


i plik Index

Kod
include_once 'class/Tables.php';
include_once 'class/TopAds.php';
include_once 'config/config.php';
include_once 'libs/Smarty.class.php';

class Enoclegi {
    private $smarty;
    private $topAds;
    private $tables;
    private $config;
    
    //constructor
    public function Enoclegi() {
        $this->smarty     =    new Smarty();
        $this->config    =    new Config();
        $this->topAds    =    new TopAds();
        $this->tables    =    new Tables();
    }
    
    public function init(){
        //$this->topAds    ->        lastAdd();        
        $this->config     ->              changeSite($siteName);
        $this->smarty    ->        assign('VoivodeshipShow', $this->tables->getVoivodeship());
        $this->smarty    ->        assign('CategoryShow', $this->tables->getCategory());
        $this->smarty    ->        display('tpl/index.tpl');
    }
}



jak sprawić aby zmienna $siteName z configu została przekazana do
Kod
$this->config     ->      changeSite($siteName);
-Konki-
Przecież tu jest wszystko źle. Nie rozumiem jak to by miało działać ;-)
-Konki-
Spróbuj tak:
  1. class Config{
  2. public function changeSite($siteName){
  3. //$siteName = 'test';
  4. echo 'mam siteName: --'.$siteName.'--<br/>';
  5. return $siteName;
  6. }
  7. }


wywołanie
  1. include_once 'class/Tables.php';
  2. include_once 'class/TopAds.php';
  3. include_once 'config/config.php';
  4. include_once 'libs/Smarty.class.php';
  5.  
  6. class Enoclegi {
  7. private $smarty;
  8. private $topAds;
  9. private $tables;
  10. private $config;
  11.  
  12. //constructor
  13. public function Enoclegi() {
  14. $this->smarty = new Smarty();
  15. $this->config = new Config();
  16. $this->topAds = new TopAds();
  17. $this->tables = new Tables();
  18. }
  19.  
  20. public function init(){
  21. //$this->topAds -> lastAdd();
  22. $this->config -> changeSite($_SERVER['REQUEST_URI']);
  23. $this->smarty -> assign('VoivodeshipShow', $this->tables->getVoivodeship());
  24. $this->smarty -> assign('CategoryShow', $this->tables->getCategory());
  25. $this->smarty -> display('tpl/index.tpl');
  26. }
  27. }


Pisane na klolanie i nie testowane.

Podstawowe błędy:
1. W wywołaniu które miałeś ($this->config -> changeSite($siteName)wink.gif jest $siteName które nigdzie nie jest zdefiniowane
2. Funkcja changeSite nie ma parametru a wywołujesz z parametrem

Pozdrawiam,
Konki
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.