« Return to Thread: Call to object function, want to PHP interpret returned string

Re: Re: Call to object function, want to PHP interpret returned string

by John Allsopp :: Rate this Message:

Reply to Author | View in Thread

Stuart wrote:

> 2009/7/6 John Allsopp <john@...>:
>  
>> David Robley wrote:
>>    
>>> John Allsopp wrote:
>>>
>>>
>>>      
>>>> Hi
>>>>
>>>> At the top of a webpage I have:
>>>>
>>>> <?php
>>>> include_once("furniture.php");
>>>> $myFurniture = new furniture();
>>>> echo $myFurniture->getTop("my company title");
>>>> ?>
>>>>
>>>> to deliver the first lines of HTML, everything in HEAD and the first
>>>> bits of page furniture (menu, etc).
>>>>
>>>> In the furniture object in getTop(), I want to return a string that
>>>> includes the CSS file that I call with an include_once. But the
>>>> include_once isn't interpreted by PHP, it's just outputted. So from:
>>>>
>>>>        $toReturn = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0
>>>> Transitional//EN' ........
>>>>            <?php
>>>>                include_once('styles3.txt');
>>>>            ?>
>>>>            .......";
>>>>
>>>>        return $toReturn;
>>>>
>>>> I get
>>>>
>>>> <?php
>>>> include_once('styles3.txt');
>>>> ?>
>>>>
>>>> in my code.
>>>>
>>>> Do I really have to break up my echo $myFurniture->getTop("my company
>>>> title"); call to getTopTop, then include my CSS, then call getTopBottom,
>>>> or can I get PHP to interpret that text that came back?
>>>>
>>>> PS. I may be stupid, this may be obvious .. I don't program PHP every day
>>>>
>>>> Thanks in advance for your help :-)
>>>>
>>>> Cheers
>>>> J
>>>>
>>>>        
>>> First guess is that your page doing the including doesn't have a filename
>>> with a .php extension, and your server is set to only parse php in files
>>> with a .php extension.
>>>
>>>
>>>
>>> Cheers
>>>
>>>      
>> Ah, thanks. It's a PHP object returning a string, I guess the PHP
>> interpreter won't see that.
>>
>> So, maybe my object has to write a file that my calling file then includes
>> after the object function call. Doesn't sound too elegant, but is that how
>> it's gotta be?
>>    
>
> You appear to be looking for the eval function: http://php.net/eval
>
> However, in 99.99% of cases using eval is not the right solution. In
> your case there are two ways to solve it.
>
> The first way, assuming the thing you're trying to include is a
> stylesheet, is to use an external link to a CSS file. That would be
> the "normal" way to include a stylesheet in an HTML page and is far
> more efficient that including it inline.
>
> If it's not just a stylesheet that you're including then you'll want
> to load the file in the getTop method. For example...
>
> $toReturn = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0
> Transitional//EN' ........";
> $toReturn.= file_get_contents('styles3.txt');
> $toReturn.= '..........';
>
> Simple as that.
>
> -Stuart
>
>  
Thanks guys. Yes, actually file_get_contents didn't work for me, and yes
you're right, of course I should be including my CSS like <LINK
rel='stylesheet' type='text/css' media='screen' href='style3.css'
title='style1'> in the header.

The style3.txt file I was trying to PHP include was there so I could
include more than one stylesheet and make just one amendment. One for
printing and I'm guessing one for mobile. All that file contained was
the <LINK... line above.

That was legacy code. Now I have a furniture object, of course, I can
put my stylesheet code in one place there just as part of the header,
and have no need for style3.txt.

Thanks for all your help.
J

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

 « Return to Thread: Call to object function, want to PHP interpret returned string