vanuit een class een andere class aanroepen

Status
Niet open voor verdere reacties.

leuthrick

Gebruiker
Lid geworden
17 sep 2008
Berichten
454
hey,

ik heb deze class om tpl files uit te lezen:

class.tpl.php
PHP:
<?php
class TemplateParser {
     var $file = 'start.tpl'; //Het standaard bestand, deze gebruikt hij als er geen bestand wordt opgegeven
     var $errors = ''; //Hier komen de errors in
                var $getfile = false; //Komt nog...
                var $tpl = ''; //Komt nog...
                var $blockcontent = array(); //Komt nog...



    public function __construct($file = false) {
     if($file != "" && $file != false) { //Kijken of $file niet leeg is.
      if(!preg_match("#(.+?).tpl#si", $file)) { //Kijken of het bestand de exentie .tpl heeft
        $this->errors .= "<b>TemplateParser Error:</b> Het bestand moet de exentie .tpl hebben!<br />";
        }
        if(!file_exists($file)) { //Kijken of het bestand bestaat
        $this->errors .= "<b>TemplateParser Error:</b> het bestand ".$file." bestaat niet!<br />";
        }
    
        $this->file = $file;
        
    
        
    }
}
public function getfile() {
        if(file_exists($this->file)) { //Kijken of bestand bestaat
            $this->tpl = file_get_contents($this->file); //Inhoud verkrijgen
            $this->getfile = true; //Bestand is opgehaald
        }
}
     public function getBlock($blockname) {
    $regex = "#\[start-block ".$blockname."\](.+?)\[end-block ".$blockname."\]#s";
    preg_match($regex, $this->tpl, $matches); //Inhoud verkrijgen
    return $matches[1]; //Inhoud returnen
}
    public function newBlock($blockname, $content) {
        $this->blockcontent[$blockname] .= $this->getBlock($blockname); //Block inhoud ophalen
        
        foreach($content as $pattern=>$replacement){                        
$this->blockcontent[$blockname] = preg_replace("#\{".$pattern."\}#si", $replacement, $this->blockcontent[$blockname]);  //Inhoud in block veranden            
        }
        
        
    }
    public function set($pattern, $replacement) {
        if($this->getfile == false) { //Kijken of het bestand al gelezen is.
        $this->getfile(); //Zo niet, bestand inlezen.
        }
        $this->tpl = preg_replace("#\{".$pattern."\}#si", $replacement, $this->tpl); //{iets} wordt veranderd in iets.
    }
public function parse() {
      if($this->errors == '') { //Kijken of de errors leeg zijn
       if($this->getfile == false) { //Als het bestand nog niet gelezen is, laten lezen.
        $this->getfile();
        } 
                  foreach($this->blockcontent as $blockname=>$block) {
            $regex = "#\[start-block ".$blockname."\](.+?)\[end-block ".$blockname."\]#s";
            $this->tpl = preg_replace($regex, $block, $this->tpl); //De inhoud aan de pagina toevoegen.
        }
                return $this->tpl; //De inhoud returnen
        }else{
            return $this->errors; //Als er errors zijn, deze returnen.
        }
    }

}
?>

class.gastenboek.php
PHP:
<?php
class gastenboek {
	public function test(){
		$template->newBlock("showpost", array('auteur'=>'test', 'datum'=>'20-2-2010', 'tijd'=>'12:43', 'bericht'=>'Dit is een test bericht.'));
	}
}
?>

index.php
PHP:
<?php
require("include/class/class.tpl.php");
require("include/class/class.gastenboek.php");
include("include/settings.php");
$template = new TemplateParser("template/standaard/index.tpl"); //Bestand index.tpl verkrijgen
$template->set("titel", "Titel van de pagina"); //titel veranderen
$template->set("tekst", "Tekst van de pagina"); //tekst veranderen
$gastenboek = new gastenboek();
$gastenboek->test();
echo $template->parse();
?>

ik krijg deze error:
Fatal error: Call to a member function newBlock() on a non-object in C:\xampp\htdocs\tpl\include\class\class.gastenboek.php on line 4

dat is deze regel:
PHP:
$template->newBlock("showpost", array('auteur'=>'test', 'datum'=>'20-2-2010', 'tijd'=>'12:43', 'bericht'=>'Dit is een test bericht.'));

alvast bedankt

Mvg leuthrick
 
Komt omdat die class niet bekend is binnen de gastenboek-class.

Je zou dus een instantie van de template class moeten maken binnen je gastenboek-class
 
Nu ik het wat beter bekijk kun je beter in je gastenboek klasse een functie maken die gegevens teruggeeft.

Dan wordt het zoiets:
class.gastenboek.php
PHP:
<?php
class gastenboek {
    public function test(){
        return array('auteur'=>'test', 'datum'=>'20-2-2010', 'tijd'=>'12:43', 'bericht'=>'Dit is een test bericht.');
    }
}
?>

index.php
PHP:
<?php
require("include/class/class.tpl.php");
require("include/class/class.gastenboek.php");
include("include/settings.php");
$template = new TemplateParser("template/standaard/index.tpl"); //Bestand index.tpl verkrijgen
$gastenboek = new gastenboek();

$template->set("titel", "Titel van de pagina"); //titel veranderen
$template->set("tekst", "Tekst van de pagina"); //tekst veranderen
$template->newBlock("showpost", $gastenboek->test());

echo $template->parse();
?>
 
oke nu loop ik tegen het volgende probleem:

hij moet de posts terug geven, dus hij moet meerdere keren
PHP:
array('auteur'=>'test', 'datum'=>'20-2-2010', 'tijd'=>'12:43', 'bericht'=>'Dit is een test bericht.');
geven.


ik had deze code:

PHP:
class gastenboek {
    public function showpost($showpostid){
        while($i != 5){
        	return array('auteur'=>'test', 'datum'=>'20-2-2010', 'tijd'=>'12:43', 'bericht'=>'Dit is een test bericht.');
        	$i += 1;
        }
    }
}

alleen return geeft maar 1 resultaat terug.

alvast bedankt
 
PHP:
class gastenboek {
    public function showpost($showpostid){
        $aPosts = array();
        while($i != 5){
        	$aPosts[] = array('auteur'=>'test', 'datum'=>'20-2-2010', 'tijd'=>'12:43', 'bericht'=>'Dit is een test bericht.');
        	$i += 1;
        }
        return $aPosts;
    }
}
 
ik krijg deze errors:

Warning: preg_replace() [function.preg-replace]: Parameter mismatch, pattern is a string while replacement is an array in C:\xampp\htdocs\tpl\include\class\class.tpl.php on line 41

Warning: preg_replace() [function.preg-replace]: Parameter mismatch, pattern is a string while replacement is an array in C:\xampp\htdocs\tpl\include\class\class.tpl.php on line 41

Warning: preg_replace() [function.preg-replace]: Parameter mismatch, pattern is a string while replacement is an array in C:\xampp\htdocs\tpl\include\class\class.tpl.php on line 41

Warning: preg_replace() [function.preg-replace]: Parameter mismatch, pattern is a string while replacement is an array in C:\xampp\htdocs\tpl\include\class\class.tpl.php on line 41

Warning: preg_replace() [function.preg-replace]: Parameter mismatch, pattern is a string while replacement is an array in C:\xampp\htdocs\tpl\include\class\class.tpl.php on line 41

met deze code in index.php

PHP:
$template->newBlock("showpost", $gastenboek->showpost());
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan