PHP类不会在适当的地方回显另一个php页面 [英] PHP class doesn't echo another php page at the appropriate place

查看:153
本文介绍了PHP类不会在适当的地方回显另一个php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我使用一个页面创建类,我发现如下,但是当我想使用一个php页面 - 再次包括和使用类文件 - 内容,它回声在顶部或底部页面...我甚至试图使页面一个函数(),并调用它的$ Content字符串,但没有使用,它再次在页面的顶部...如何使用PHP页面作为内容这个类,或者我应该改变为使用php文件?

Alright, I'm using a page creating class I found as below but when I want to use a php page -that again includes and uses a class file- for the content it either echoes on the top or the bottom of the page... I even tried to make the page a function() and call it at the $Content string but no use, again it echoed on the top of the page... How can i use a php page as a content in this class, or what should i change to use a php file?

请记住,我是新的类,所以可以随意假定一些初学者的错误。 / p>

Please keep in mind that I'm new to classes so feel free to assume some beginner mistakes.

<?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( );

?>

如果我想让内容像$ Content =<?echo'test' ;?>;我知道这是无效的,但我想做的是类似的东西,像$ Content =whateversinhere.php的输出;。

What if I wanted to make the content something like $Content = " < ? echo 'test'; ? >"; I know this isn't valid but what i'm trying to do is something like that or something like $Content = " output of the whateversinhere.php ";. how should I object orient another page therefore getting its contents into a string here?

推荐答案

你不应该在类中回显任何内容,相反,类应该有一个方法getMarkup(),它将返回一个包含整个标记的字符串。

You should NOT echo anything inside your class, instead the class should have a method getMarkup(), which will return a string containing the whole markup. Then you can echo that string in your view.

其他小费:



  • 将您的变量设置为私有的(private $ title,变量和方法名称以小写字母开头

  • <等)

这篇关于PHP类不会在适当的地方回显另一个php页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆