Re: How to insert a file in a class?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Jun 1, 2012, at 8:00 AM, Gibbs wrote:

On 01/06/12 13:41, LAMP wrote:
Hi to all.

Let's say there is a class

class Box
{
   var $box_title;
   var $box_content;

   function __construct()
   {
       $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- >box_content;
   }


   function get_box()
   {
       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


Couldn't you just do something like:

function box_content($file = NULL)
{
       $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

No. It doesn't work.
And yes, the content of the "box" could be anything.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux