Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在类外显示方法的属性_Php_Oop - Fatal编程技术网

Php 如何在类外显示方法的属性

Php 如何在类外显示方法的属性,php,oop,Php,Oop,我已经创建了一个主题类。我想通过addCss方法向html头部添加一个css文件,并使用displayCss方法显示 在我的主题类中,我创建了displayCss()方法来显示addCss()方法中的css代码,并使用addCss包含css文件。请参见head.phtml和index.php 问题是它没有在head.phtml中显示css代码 <?php $theme = new Template(); ?> <html> <title>Page</ti

我已经创建了一个主题类。我想通过addCss方法向html头部添加一个css文件,并使用displayCss方法显示

在我的主题类中,我创建了
displayCss()
方法来显示
addCss()
方法中的css代码,并使用addCss包含css文件。请参见head.phtml和index.php

问题是它没有在head.phtml中显示css代码

<?php $theme = new Template(); ?>
<html>
<title>Page</title>

<?php $theme->displayCss(); ?>
另外一个问题是我在index.php中调用模板类,为什么我必须在head.phtml中再次调用它。如果我不在head.phtml中调用它,我会得到这个错误

>  Undefined variable: theme in head.phtml
谢谢

Class Template {

    public function __construct ( $template_dir = null, $css_dir = null, $js_dir = null ) {

    if ( $template_dir !== null ) 
        $this->template_dir = $template_dir;
    if ( $css_dir !== null ) 
        $this->css_dir = $css_dir;
    if ( $js_dir !== null ) 
        $this->js_dir = $js_dir;
}

public function render ( $template_file ) {

    if ( file_exists ( $this->template_dir.$template_file ) ){
        include $this->template_dir.$template_file;
    } else {
        throw new Exception ( 'template dosyalari eksik ' . $this->template_dir . $template_file  ); 
    }
}

public function displayCss () {
    echo $this->CSS;
}

public function htmlHead () {
    self::render('head.phtml');
}

public function addCss( $css_file )
{
    if ( preg_match('/http/i', $css_file ) ) {
        $this->CSS = sprintf( '<link rel="stylesheet" href="%s">', $css_file );
    }else{

        if ( file_exists ( $this->css_dir.$css_file )  ) {
            $this->CSS = sprintf( '<link rel="stylesheet" href="%s">', $this->css_dir . $css_file );
        } else {
            $this->CSS = "css dosyasina ulasilamiyor";
        }
    }
    return $this->CSS;
}

}
类模板{
公共函数构造($template\u dir=null,$css\u dir=null,$js\u dir=null){
如果($template_dir!==null)
$this->template\u dir=$template\u dir;
如果($css_dir!==null)
$this->css\u dir=$css\u dir;
如果($js_dir!==null)
$this->js_dir=$js_dir;
}
公共函数呈现($template\u文件){
如果(文件存在($this->template\u dir.$template\u file)){
包括$this->template\u dir.$template\u文件;
}否则{
抛出新异常('template dosylari eksik'.$this->template\u dir.$template\u file);
}
}
公共函数displayCss(){
echo$this->CSS;
}
公共函数htmlHead(){
self::render('head.phtml');
}
公共函数addCss($css\u文件)
{
if(preg_匹配('/http/i',$css_文件)){
$this->CSS=sprintf(“”,$CSS\u文件);
}否则{
如果(文件存在($this->css\u dir.$css\u file)){
$this->CSS=sprintf(“”,$this->CSS\u dir.$CSS\u文件);
}否则{
$this->CSS=“CSS dosysina ulasilamiyor”;
}
}
返回$this->CSS;
}
}
head.phtml

<?php $theme = new Template(); ?>
<html>
<title>Page</title>

<?php $theme->displayCss(); ?>
index.php

<?php

 require_once 'template.class.php';
 $theme = new Template();

 $theme->addCss('style.css');

 $theme->htmlHead();

仅澄清类和对象的区别。使用self::我们在类定义中称之为静态函数的东西。
当我们从类定义内部调用对象函数或属性时,我们使用$this关键字。因此函数htmlHead()必须是:

public function htmlHead () {
    $this->render('head.phtml');
}
<html>
<title>Page</title>

<?php $this->displayCss(); ?>
根据上述规定,head.phtml必须:

public function htmlHead () {
    $this->render('head.phtml');
}
<html>
<title>Page</title>

<?php $this->displayCss(); ?>
因为您在类定义中包括head.phtml,所以调用
$theme->htmlHead();
而且您不需要创建新的模板对象。

为什么要投反对票?