PHP类没有';t在适当的位置回显另一个php页面

PHP类没有';t在适当的位置回显另一个php页面,php,class,Php,Class,好的,我正在使用一个页面创建类,我发现如下,但当我想使用一个php页面时——它再次包含并使用一个类文件——它的内容要么在页面顶部或底部回响。。。我甚至尝试将页面设置为function(),并在$Content字符串处调用它,但没有用,它再次在页面顶部回响。。。如何将php页面用作此类中的内容,或者使用php文件时应更改哪些内容 请记住,我是新手,所以请随意假设一些初学者的错误 <?php class Page { var $Title; var $Keywords; var $Conten

好的,我正在使用一个页面创建类,我发现如下,但当我想使用一个php页面时——它再次包含并使用一个类文件——它的内容要么在页面顶部或底部回响。。。我甚至尝试将页面设置为function(),并在$Content字符串处调用它,但没有用,它再次在页面顶部回响。。。如何将php页面用作此类中的内容,或者使用php文件时应更改哪些内容

请记住,我是新手,所以请随意假设一些初学者的错误

<?php
class Page {
var $Title;
var $Keywords;
var $Content;

   function Display( ) {
 echo "<HTML>\n<HEAD>\n";
 $this->DisplayTitle( );
 $this->DisplayKeywords( );
 echo "\n</HEAD>\n<BODY>\n";
 echo $this->Content;
 echo "\n</BODY>\n</HTML>\n";
}

   function DisplayTitle( ) {
 echo "<TITLE>" . $this->Title . "</TITLE>\n";
   }

   function DisplayKeywords( ) {
     echo '<META NAME="keywords" CONTENT="' . $this->Keywords . '">';
   }

   function SetContent( $Data ) {
     $this->Content = $Data;
   }
 }
?>

用法:

<?php
include "page.class";

$Sample = new Page;

$Content = "<P>I want my php file's contents here.</P>";

$Sample->Title = "Using Classes in PHP";
$Sample->Keywords = "PHP, Classes";
$Sample->SetContent( $Content );

$Sample->Display( );

?>

您不应该在类中回显任何内容,相反,该类应该有一个方法getMarkup(),该方法将返回一个包含整个标记的字符串。然后可以在视图中回显该字符串

其他TIPP:

  • 变量和方法名称以小写字母开头
  • 标题和关键字也应该有getter和setter
  • 使变量私有化(私有$title等)
    • 使用


      让我为您清理一下,您会注意到一些变化:

      class Page
      {
          private $title = 'No Title';
          private $keywords = array();
          private $content = '';
      
          public function setTitle($title)
          {
              $this->title = (string)$title;
          }
      
          public function addKeywords($keywords)
          {
              $this->keywords = array_merge($this->keywords, (func_num_args() > 1) ? func_get_args() : (array)$keywords;
          }
      
          function setContent($content)
          {
              $this->content = $content;
          }
      
          function appendContent($content)
          {
              $this->content .= $content;
          }
      
          function prependContent($content)
          {
              $this->content = $content . $this->content;;
          }
      
          private function display()
          {
              /*
               * Display output here
               */
              echo $this->title;
              echo implode(',',str_replace(',','',$this->title));
              echo $this->contents;
          }
      }
      
      非常简单的用法:

      $Page = new Page;
      $Page->setTitle("Hello World");
      $page->addKeywords("keyword1","keyword2","keyword3","keyword4");
      
      //Content
      $this->setContent("World");
      $this->prependContent("Hello");
      $this->appendContent(".");
      
      //Display
      $this->display();
      

      只要填空,随着时间的推移,你就会知道你不应该在你的类中直接使用html,你会把上面的内容分成几个类,比如
      Doctype
      ,并有一个页面类将它们结合在一起。

      如果,$Content=Content();函数content()是一个php代码,它使用一个类然后返回一个值。所以从逻辑上讲,内容应该是使用其他类处理信息的php文件的内容。为什么这样不好?使用输出控制是否有问题?:(我看不出双输出问题。
      $Page = new Page;
      $Page->setTitle("Hello World");
      $page->addKeywords("keyword1","keyword2","keyword3","keyword4");
      
      //Content
      $this->setContent("World");
      $this->prependContent("Hello");
      $this->appendContent(".");
      
      //Display
      $this->display();