Re: How to insert a file in a class?
| From: | LAMP | Date: | Fri, 01 Jun 2012 13:04:06 +0000 |
| Subject: | Re: How to insert a file in a class? | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318073@lists.php.net to get a copy of this message | ||
On Jun 1, 2012, at 8:00 AM, Gibbs wrote:
On 01/06/12 13:41, LAMP wrote:No. It doesn't work. And yes, the content of the "box" could be anything.Hi to all. Let's say there is a class class Box { var $box_title; var $box_content; function __construct() {box_content;$this->box = '';} function box_title($title) {$this->title = $title;} function box_content($content) {$this->content = $content;} function make_box() {$this->box = '<h3>'.$this->box_title.'</h3>'.$this-} function get_box() {Couldn't you just do something like: function box_content($file = NULL) {return $this->box;} } $box = new Box(); $box->box_title('PHP Classes'); $box->box_content('Starting with PHP 5, the object model was rewritten to allow for better performance and more features. This was a major change from PHP 4. PHP 5 has a full object model.') $box->make_box(); echo $box->get_box(); This works fine. The problem I have is how to "include" a file as box_content? it could be plain text, but it could be a form or some kind of code. $box->box_include(include(/path/to/file/file.php)) doesn't work, of course. Wrapping up the whole code in a variable doesn't make a sense too: # file.php $content = ' <form method="post" action="$_SERVER['PHP_SELF']">Email = <input type=text name=email> Pass = <input type=password name=pass><input type=submit value=Submit> </form>'; # main.php $box = new Box(); $box->box_title('PHP Classes'); include(file.php); $box->box_content($content); $box->make_box(); echo $box->get_box(); Also, I'm sure I read once it's not correct to print directly from a class. First return a value/result to "main" code and then print. Correct? LAMP$content = file_exists($file) ? include($file) : NULL; $this->content = $content;} It really depends what is being included (text, HTML, PHP etc). Personally I would create a different method for each different type as they will have to be treated and returned differently. Gibbs