php-general Digest 10 Nov 2010 04:41:33 -0000 Issue 7030
Topics (messages 309400 through 309421):
Re: Template engines
309400 by: tedd
309401 by: tedd
309402 by: tedd
309405 by: Robert Cummings
309406 by: tedd
309408 by: Robert Cummings
309410 by: knl.bitflop.com
309411 by: Adam Richardson
309421 by: Govinda
Chat
309403 by: DuÅ¡an NovakoviÄ
309407 by: Steve Staples
Re: Newbie looking for a project
309404 by: tedd
Re: Basic question
309409 by: Daniel P. Brown
Re: Is session_start() using encrypted cookies with HTTPS
309412 by: Adam Richardson
309413 by: Bastien Koert
Updating a GET variable
309414 by: Marc Guay
309415 by: [email protected]
309416 by: Marc Guay
309417 by: Marc Guay
309419 by: admin.buskirkgraphics.com
Help with variable variables not being set for a multi-dimensional array
309418 by: Daevid Vincent
309420 by: Daevid Vincent
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
At 10:24 PM -0500 11/8/10, Matt Giddings wrote:
I have to agree with David here. Smarty may be an added layer to the whole
mix, but it makes maintaining the template and application much simpler.
Much better than mixing html/php all together in a single file. That and a
single template is easily used in multiple applications if needed, ore in
multiple files within the same application. IMHO the benefits far out way
the negatives of using a template engine.
-1
To each their own.
Cheers,
tedd
--
-------
http://sperling.com/
--- End Message ---
--- Begin Message ---
At 12:20 AM -0500 11/9/10, Paul M Foster wrote:
On Mon, Nov 08, 2010 at 02:41:12PM -0700, Hansen, Mike wrote:
I really like the idea of using a templating engine. Which one do you
use? Why? For those that don't use templating engines, why don't you
use them?
Here's why I don't use a templating engine.
First, You're taking an embedded language and writing a templating
engine in it. Seriously?
Second, you're painting a single page, maybe 10-15K worth of
information, and you're dragging 100K of code around just to paint a
single page. Seriously?
Third, you're creating a page on the fly, thrashing the server's CPU in
order to generate a page which could be built statically, or at least
with minimally embedded PHP. Seriously?
Fourth, you could achieve the same end result with carefully crafted PHP
embedded in your static HTML file, with actually very little more work.
I know, everyone's heard this before. But just because you *can* do
something doesn't mean you *should*.
Paul
+1
Cheers,
tedd
--
-------
http://sperling.com/
--- End Message ---
--- Begin Message ---
At 12:23 AM -0500 11/9/10, Robert Cummings wrote:
On 10-11-09 12:20 AM, Paul M Foster wrote:
On Mon, Nov 08, 2010 at 02:41:12PM -0700, Hansen, Mike wrote:
I really like the idea of using a templating engine. Which one do you
use? Why? For those that don't use templating engines, why don't you
use them?
Here's why I don't use a templating engine.
First, You're taking an embedded language and writing a templating
engine in it. Seriously?
Second, you're painting a single page, maybe 10-15K worth of
information, and you're dragging 100K of code around just to paint a
single page. Seriously?
Third, you're creating a page on the fly, thrashing the server's CPU in
order to generate a page which could be built statically, or at least
with minimally embedded PHP. Seriously?
Fourth, you could achieve the same end result with carefully crafted PHP
embedded in your static HTML file, with actually very little more work.
I know, everyone's heard this before. But just because you *can* do
something doesn't mean you *should*.
Aaaah, the great argument that uses a single brush to paint all
template engines into one corner because if you've used one, you've
used them all... Seriously?
Cheers,
Rob.
I don't have to try them all, just the most popular. If the most
popular doesn't work for me, then I don't need to try any others.
Cheers,
tedd
--
-------
http://sperling.com/
--- End Message ---
--- Begin Message ---
On 10-11-09 10:42 AM, tedd wrote:
At 12:23 AM -0500 11/9/10, Robert Cummings wrote:
On 10-11-09 12:20 AM, Paul M Foster wrote:
On Mon, Nov 08, 2010 at 02:41:12PM -0700, Hansen, Mike wrote:
I really like the idea of using a templating engine. Which one do you
use? Why? For those that don't use templating engines, why don't you
use them?
Here's why I don't use a templating engine.
First, You're taking an embedded language and writing a templating
engine in it. Seriously?
Second, you're painting a single page, maybe 10-15K worth of
information, and you're dragging 100K of code around just to paint a
single page. Seriously?
Third, you're creating a page on the fly, thrashing the server's CPU in
order to generate a page which could be built statically, or at least
with minimally embedded PHP. Seriously?
Fourth, you could achieve the same end result with carefully crafted PHP
embedded in your static HTML file, with actually very little more work.
I know, everyone's heard this before. But just because you *can* do
something doesn't mean you *should*.
Aaaah, the great argument that uses a single brush to paint all
template engines into one corner because if you've used one, you've
used them all... Seriously?
Cheers,
Rob.
I don't have to try them all, just the most popular. If the most
popular doesn't work for me, then I don't need to try any others.
That's all well and good, but the argument being used is a
generalization which at best is wrong. I don't care what you use, but
FUD doesn't help anything.
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--- End Message ---
--- Begin Message ---
At 10:57 AM -0500 11/9/10, Robert Cummings wrote:
On 10-11-09 10:42 AM, tedd wrote:
I don't have to try them all, just the most popular. If the most
popular doesn't work for me, then I don't need to try any others.
That's all well and good, but the argument being used is a
generalization which at best is wrong. I don't care what you use,
but FUD doesn't help anything.
Cheers,
Rob.
I disagree and what I said is not FUD.
If we took the time to *not* generalize, then we would be up to our
collective butts in evaluations of everything, while getting nothing
accomplished.
Generalization is a way to navigate the massive amounts of data we
are bombarded with each day. Do I want to spend/waste time evaluating
all templates to the nth degree? I think not.
Instead, I research ideas and see if they meet with my way of doing
things. If they do, then I incorporate them into my method -- if not,
then I pass.
I have changed my methods many times to accommodate new technologies
and new ways of doing things, but that does not mean that I always
incorporate everything that comes along. Templates are one of those
things that do not fit in my way of doing things and I do not need to
investigate all template systems to arrive at that conclusion. As
such, there is nothing wrong with my evaluation method for it works
for me.
Cheers,
tedd
--
-------
http://sperling.com/
--- End Message ---
--- Begin Message ---
On 10-11-09 11:18 AM, tedd wrote:
At 10:57 AM -0500 11/9/10, Robert Cummings wrote:
On 10-11-09 10:42 AM, tedd wrote:
I don't have to try them all, just the most popular. If the most
popular doesn't work for me, then I don't need to try any others.
That's all well and good, but the argument being used is a
generalization which at best is wrong. I don't care what you use,
but FUD doesn't help anything.
Cheers,
Rob.
I disagree and what I said is not FUD.
If we took the time to *not* generalize, then we would be up to our
collective butts in evaluations of everything, while getting nothing
accomplished.
Generalization is a way to navigate the massive amounts of data we
are bombarded with each day. Do I want to spend/waste time evaluating
all templates to the nth degree? I think not.
Instead, I research ideas and see if they meet with my way of doing
things. If they do, then I incorporate them into my method -- if not,
then I pass.
I have changed my methods many times to accommodate new technologies
and new ways of doing things, but that does not mean that I always
incorporate everything that comes along. Templates are one of those
things that do not fit in my way of doing things and I do not need to
investigate all template systems to arrive at that conclusion. As
such, there is nothing wrong with my evaluation method for it works
for me.
I think the key quote here is "your way". Others will certainly have
different mileage (as has already been expressed), thus your
generalization still rings false. It's only a generalization for you and
others of like mind... which is terribly partisan :)
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.
--- End Message ---
--- Begin Message ---
On Tue, 9 Nov 2010 00:20:52 -0500
Paul M Foster <[email protected]> wrote:
> On Mon, Nov 08, 2010 at 02:41:12PM -0700, Hansen, Mike wrote:
>
> Here's why I don't use a templating engine.
>
> First, You're taking an embedded language and writing a templating
> engine in it. Seriously?
>
> Second, you're painting a single page, maybe 10-15K worth of
> information, and you're dragging 100K of code around just to paint a
> single page. Seriously?
>
> Third, you're creating a page on the fly, thrashing the server's CPU
> in order to generate a page which could be built statically, or at
> least with minimally embedded PHP. Seriously?
>
> Fourth, you could achieve the same end result with carefully crafted
> PHP embedded in your static HTML file, with actually very little more
> work.
>
> I know, everyone's heard this before. But just because you *can* do
> something doesn't mean you *should*.
Beautiful put!
---
Mange venlige hilsner/Best regards
Kim N. Lesmer
Programmer/Unix systemadministrator
Web : www.bitflop.com
E-mail : [email protected]
> Paul
>
> --
> Paul M. Foster
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--- End Message ---
--- Begin Message ---
On Mon, Nov 8, 2010 at 4:41 PM, Hansen, Mike <[email protected]> wrote:
> I really like the idea of using a templating engine. Which one do you use?
> Why? For those that don't use templating engines, why don't you use them?
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
I prefer using what essentially amounts to a templating engine, my web
framework, Nephtali. It's markup conventions can be found at the link
below:
http://nephtaliproject.com/documentation/markup/
It helps me easily get things right, for example:
1. Appropriate escaping of output, automatically examining the context to
determine the appropriate form of escaping (html, attribute, or url), or I
can manually declare escaping in the markup to improve performance.
2. When an error occurs in one dynamic region of the page, I don't always
want to show a generic error page (I'd rather just show an error message for
that particular region that experienced an error and show the rest of the
page normally.)
3. I like to progressively enhance pages, and sometimes this involves
grabbing the content of one particular dynamic region of a page. Nephtali
builds in a restful API that allows you to retrieve the content of a dynamic
region instead of retrieving the whole page (the processing short-circuits
to improve performance), for example:
http://nephtaliproject.com/php53hosts/index.php?nmode=htmlfrag&npipe=hosts
4. I like to work on projects with many others of various skill levels
and backgrounds, and the xml comments are easily understood and play nicely
with a broad range of editors/IDE's.
5. Nephtali plays nicely with legacy PHP code, so you can add it to
existing sites without issue (as long as turning on output buffering isn't
an issue.) So, if you want to add some PHP to the page, no problem, you can
have the best of both worlds.
6. Nephtali offers more features (input validation, PDO wrappers, etc.)
and fits within one file, so I get a lot without much ado.
Now, I framed this in terms of my web framework because it's what I know
best. That said, my documentation isn't up to par yet, so it would likely
be frustrating getting up to speed (I did build a code generator to help
out, though.)
Smarty, PHPTAL (I especially appreciate its syntax), and other templating
engines are well documented and offer many of the same features and
qualities, and those features are what compel me to use them as opposed to
just using PHP. That doesn't mean I always use a templating
engine/framework, but I use one much more frequently than not.
Adam
--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com
--- End Message ---
--- Begin Message ---
I really like the idea of using a templating engine. Which one do
you use? Why? For those that don't use templating engines, why don't
you use them?
Hi Mike
I am still so new at PHP that i do not get a real valid vote here...
(I have never used any templating engine).. BUT I know myself well
enough to know that I am never satisfied in any coding situation
unless I know just exactly what all is really going on under the
hood. I am one of the slower meticulous coders as a result, but I
always enjoy the feeling (as do my clients) that I can do whatever I
want, ... whatever they need. I am looking forward to getting to this
level with PHP in coming months/years.
I would not bother with a templating engine or any framework until I
could have written it myself, if I had wanted.. whether or not I
actually do, in the end.
-Govinda
--- End Message ---
--- Begin Message ---
Hello there,
I have to make chat for website that has around 10 000 users (small
social network). So before I start, I would like to hear different
opinions. Important thing is to have in mind that in one moment you
can have over 1 000 users using chat.
So, if you have time fill free to write you experience in this field,
suggestions, etc.
Thnx,
Dusan
--
Please consider the environment before printing this email.
--- End Message ---
--- Begin Message ---
On Tue, 2010-11-09 at 16:51 +0100, Dušan Novaković wrote:
> Hello there,
>
> I have to make chat for website that has around 10 000 users (small
> social network). So before I start, I would like to hear different
> opinions. Important thing is to have in mind that in one moment you
> can have over 1 000 users using chat.
> So, if you have time fill free to write you experience in this field,
> suggestions, etc.
>
> Thnx,
> Dusan
>
>
>
> --
>
>
> Please consider the environment before printing this email.
>
I did that for a site for my buddy... i tried a few different ways, and
the current way seems to work decently... but i've been thinking about
altering it...
what i did for that one, is it inserts into a database what the person
wrote, then on eth other side, it pulls every x seconds from the db, and
using ajax adds to the <div>. it is a pain in the ass, but yeah, it
works. I also had a flag for "typing" indicator.
i am sure there are many different ways to do this, but posting to a db,
and pulling from the db seemed to be the logical way to do it when i did
that... my buddies old way of doing it, is at http://radiokaos.com
it just posts to the db, and then refreshed the iframe every 20 seconds
or something.
--- End Message ---
--- Begin Message ---
At 12:34 PM -0500 11/8/10, Daniel P. Brown wrote:
On Mon, Nov 8, 2010 at 06:29, Ashim Kapoor <[email protected]> wrote:
Writing apps on my own is fun but it's fruit is only for me to benefit
from,but yes if nothing else I should do that.
Not at all, many others can benefit from it as well. Tedd's
examples have been referenced on this list many times, and you can see
them yourself:
http://www.php1.net/
Just because you're developing the code to learn for yourself
doesn't mean you can't put it in the public domain for others to do
the same.
--
</Daniel P. Brown>
Thanks for the plug, but let me add this.
When you develop a demo for yourself, you can take liberties.
However, when you release a demo for public/peer review, you had
better know what you are doing and that makes you a better programmer.
My advice, spend your time learning and helping others -- it will educate both.
Cheers,
tedd
--
-------
http://sperling.com/
--- End Message ---
--- Begin Message ---
On Tue, Nov 9, 2010 at 02:55, Tseveendorj Ochirlantuu
<[email protected]> wrote:
> Hello,
>
> I would like to know what is the difference between follows.
>
> PHP 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli) (built: Sep 17 2010 13:49:46)
>
>
> and
>
> PHP 5.1.6-pl6-gentoo (cgi-cgi)
They are completely and totally different in almost every way.
The first:
* PHP 5.3.2
* Command Line Interface
* Built for Ubuntu
* Has the Suhosin patch applied
The second:
* PHP 5.1.6
* Common Gateway Interface
* Built for Gentoo
For further explanations, remember that Google is your friend.
--
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/
--- End Message ---
--- Begin Message ---
>
> If the cookie needs to be encrypted, why not just encrypt it and worry less
> about the transport layer? Or just down one hash value id cookie and pull
> back the secure data for action just on the server?
>
> Bastien
The issue highlighted in Yannick's question wouldn't be resolved by merely
encrypting the cookie value.
Encrypting a cookie value protects the value encrypted, and for some
situations this is exactly what you want. Maybe you're storing preferences
for your app, but want to make sure they aren't tampered with, etc.
However, encrypting a cookie that's used as an auth token won't buy you
anything if the transport layer doesn't provide encryption. That's because
an auth token mere presence works to sufficiently identify an authenticated
user. I don't have to know what the value in the cookie means in any way.
Does this help clear up your question, or did I misunderstand you, Bastien?
Adam
--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com
--- End Message ---
--- Begin Message ---
On Tue, Nov 9, 2010 at 2:10 PM, Adam Richardson <[email protected]> wrote:
>>
>> If the cookie needs to be encrypted, why not just encrypt it and worry less
>> about the transport layer? Or just down one hash value id cookie and pull
>> back the secure data for action just on the server?
>>
>> Bastien
>
>
> The issue highlighted in Yannick's question wouldn't be resolved by merely
> encrypting the cookie value.
>
> Encrypting a cookie value protects the value encrypted, and for some
> situations this is exactly what you want. Maybe you're storing preferences
> for your app, but want to make sure they aren't tampered with, etc.
>
> However, encrypting a cookie that's used as an auth token won't buy you
> anything if the transport layer doesn't provide encryption. That's because
> an auth token mere presence works to sufficiently identify an authenticated
> user. I don't have to know what the value in the cookie means in any way.
>
> Does this help clear up your question, or did I misunderstand you, Bastien?
>
> Adam
>
> --
> Nephtali: PHP web framework that functions beautifully
> http://nephtaliproject.com
>
Nope, makes sense, Adam.
Thanks,
--
Bastien
Cat, the other other white meat
--- End Message ---
--- Begin Message ---
Hi folks,
I'm sure this is an easy one that's standing right in front of me but
I'm too blind to see. I have a page with an URL like this:
index.php?name=value&this=that. I have a link on the bottom of the
page which allows the user to switch languages. I need to hang onto
the existing $name and $this get variables.
I tried
<a href='&lang=en'>Flip</a>
and it loses all previous GET data. Then I tried
<a href='<?php echo $_SERVER['SCRIPT_NAME'] ;?>?<?php echo
$_SERVER['QUERY_STRING']; ?>&lang=en'>Flip</a>
And it continually appends more &lang=en's to the URL if the user
keeps clicking on it (for whatever reason).
Now I've put it in a form and it works as expected, but the display
value and my under-the-hood value need to be different, so it's a
fail. What's the best way to update the value of a GET var?
Marc
--- End Message ---
--- Begin Message ---
Don't think about it in those terms and you'll see why it wasn't working.
Http is stateless, so unless you use sessions, it will 'forget' everything from
page to page.
What's wrong with just putting the url parameters in the link that you know you
need, one by one? If you have many, build a little function for it to reduce
code repetition. Also, don't just output the values sent to the server, as
that's an attack waiting to happen.
Thanks,
Ash
http://www.ashleysheridan.co.uk
----- Reply message -----
From: "Marc Guay" <[email protected]>
Date: Tue, Nov 9, 2010 20:12
Subject: [PHP] Updating a GET variable
To: "php-general" <[email protected]>
Hi folks,
I'm sure this is an easy one that's standing right in front of me but
I'm too blind to see. I have a page with an URL like this:
index.php?name=value&this=that. I have a link on the bottom of the
page which allows the user to switch languages. I need to hang onto
the existing $name and $this get variables.
I tried
<a href='&lang=en'>Flip</a>
and it loses all previous GET data. Then I tried
<a href='<?php echo $_SERVER['SCRIPT_NAME'] ;?>?<?php echo
$_SERVER['QUERY_STRING']; ?>&lang=en'>Flip</a>
And it continually appends more &lang=en's to the URL if the user
keeps clicking on it (for whatever reason).
Now I've put it in a form and it works as expected, but the display
value and my under-the-hood value need to be different, so it's a
fail. What's the best way to update the value of a GET var?
Marc
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---
--- Begin Message ---
> What's wrong with just putting the url parameters in the link that you know
> you need, one by one?
I have a footer that I include on every page and would like it to
adapt to whatever situation it finds itself in. Is your suggestion,
to do the following for the existing example:
echo "<a
href='index.php?name=".$_GET['name']."&this=".$_GET['this']."&lang=en'>Flip</a>";
> Also, don't just output the values sent to the server, as that's an attack
> waiting to happen.
Are you referring to echoing the SCRIPT_NAME and QUERY STRING values
into the href attribute?
--- End Message ---
--- Begin Message ---
My working solution was to put it in a form with a hidden input with
the correct name and value.
<form method="post">
<input type='hidden' name='lang' value='<?php echo $other_lang; ?>'>
<input type='submit' value='<?php echo $other_lang_display; ?>'/>
</form>
But I'm still wondering what your advice is regarding best practice.
Marc
--- End Message ---
--- Begin Message ---
No,
Try this.
<?php
Session_start();
$_SESSION['language'] = "en";
You can set the session variable to the current get or maintain the original
passed.
May I also make a suggestion to that horrid string you presented?
<a href='<?php echo $_SERVER['SCRIPT_NAME'] ;?>?<?php echo
$_SERVER['QUERY_STRING']; ?>&lang=en'>Flip</a>
Dear god, why would you butcher php like that?
I, like many prefer short code or at least concat the string.
<a href='<?php echo
"$_SERVER[SCRIPT_NAME]"."?$_SERVER[QUERY_STRING]";?>&lang=en'>Flip</a>
Richard L. Buskirk
-----Original Message-----
From: Marc Guay [mailto:[email protected]]
Sent: Tuesday, November 09, 2010 3:48 PM
To: [email protected]
Cc: php-general
Subject: Re: [PHP] Updating a GET variable
> What's wrong with just putting the url parameters in the link that you
know
> you need, one by one?
I have a footer that I include on every page and would like it to
adapt to whatever situation it finds itself in. Is your suggestion,
to do the following for the existing example:
echo "<a
href='index.php?name=".$_GET['name']."&this=".$_GET['this']."&lang=en'>Flip<
/a>";
> Also, don't just output the values sent to the server, as that's an
attack waiting to happen.
Are you referring to echoing the SCRIPT_NAME and QUERY STRING values
into the href attribute?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--- End Message ---
--- Begin Message ---
I've used variable variables before but for some reason I can't figure this
snippet out. Why doesn't $ini_file get set (or appended to). Is there a
better more elegant solution to parsing these keys and splitting them into
sub-array keys as you see what I'm trying to do. I was thinking some
recursion, but it seems overkill for such a simple task.
Given an .ini file like so:
[production]
agis_core.adapter = Mysqli
agis_core.params.host = 10.10.10.46
agis_core.params.username = USERNAME
agis_core.params.password = PASSWORD
agis_core.params.dbname = agis_core
agis_core.params.port = 3306
I'm writing a simple parser (to be semi-compatible with the Zend Framework
one apparently but without the bloat of ZF)
require_once $path.'/../classes/IniParser.class.php';
try
{
$config_ini = new IniParser($path.'/../config.ini', true, true);
}
catch (Exception $e)
{
echo 'Caught Exception parsing ini
file.<br>'.$e->getMessage()."\n";
}
class IniParser
{
private $file;
public $ini_array;
function __construct($file, $process_sections=true, $explode=false)
{
$this->file = $file;
$this->ini_array = parse_ini_file($file,$process_sections);
if (!$this->ini_array)
{
//we only check this if we failed since Disk I/O is
expensive
if (!file_exists($file)) throw new Exception('File
Not Found: '.$file);
}
if ($explode) $this->explode_ini();
}
public function explode_ini()
{
$ini_array = array();
foreach($this->ini_array as $heading => $key_vals)
{
foreach ($key_vals as $k => $v)
{
$path = 'ini_array[\''.$heading.'\']';
$subsection = explode('.', $k);
foreach ($subsection as $ss) $path .=
'[\''.$ss.'\']';
//echo $path.' = '.$v.'<br>';
$$path = $v;
var_dump($path, $$path, $ini_array);
}
}
$this->ini_array = $ini_array;
}
}
?>
But the $ini_array is not being set?!? Here is the output I get...
string 'ini_array['production']['agis_core']['adapter']' (length=47)
string 'Mysqli' (length=6)
array
empty
string 'ini_array['production']['agis_core']['params']['host']' (length=54)
string '10.10.10.46' (length=11)
array
empty
string 'ini_array['production']['agis_core']['params']['username']'
(length=58)
string 'USERNAME' (length=7)
array
empty
...
--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Daevid Vincent [mailto:[email protected]]
> Sent: Tuesday, November 09, 2010 6:55 PM
> To: '[email protected]'
> Subject: Help with variable variables not being set for a
> multi-dimensional array
>
> I've used variable variables before but for some reason I
> can't figure this snippet out. Why doesn't $ini_file get set
> (or appended to). Is there a better more elegant solution to
> parsing these keys and splitting them into sub-array keys as
> you see what I'm trying to do. I was thinking some recursion,
> but it seems overkill for such a simple task.
>
> Given an .ini file like so:
>
> [production]
> agis_core.adapter = Mysqli
> agis_core.params.host = 10.10.10.46
> agis_core.params.username = USERNAME
> agis_core.params.password = PASSWORD
> agis_core.params.dbname = agis_core
> agis_core.params.port = 3306
>
> I'm writing a simple parser (to be semi-compatible with the
> Zend Framework one apparently but without the bloat of ZF)
>
> require_once $path.'/../classes/IniParser.class.php';
> try
> {
> $config_ini = new IniParser($path.'/../config.ini', true, true);
> }
> catch (Exception $e)
> {
> echo 'Caught Exception parsing ini
> file.<br>'.$e->getMessage()."\n";
> }
>
>
> class IniParser
> {
> private $file;
> public $ini_array;
>
> function __construct($file, $process_sections=true,
> $explode=false)
> {
> $this->file = $file;
> $this->ini_array =
> parse_ini_file($file,$process_sections);
> if (!$this->ini_array)
> {
> //we only check this if we failed since
> Disk I/O is expensive
> if (!file_exists($file)) throw new
> Exception('File Not Found: '.$file);
> }
>
> if ($explode) $this->explode_ini();
> }
>
> public function explode_ini()
> {
> $ini_array = array();
>
> foreach($this->ini_array as $heading => $key_vals)
> {
> foreach ($key_vals as $k => $v)
> {
> $path = 'ini_array[\''.$heading.'\']';
> $subsection = explode('.', $k);
> foreach ($subsection as $ss)
> $path .= '[\''.$ss.'\']';
> //echo $path.' = '.$v.'<br>';
> $$path = $v;
> var_dump($path, $$path, $ini_array);
> }
> }
>
> $this->ini_array = $ini_array;
> }
> }
> ?>
>
> But the $ini_array is not being set?!? Here is the output I get...
>
> string 'ini_array['production']['agis_core']['adapter']' (length=47)
>
> string 'Mysqli' (length=6)
>
> array
> empty
>
> string
> 'ini_array['production']['agis_core']['params']['host']' (length=54)
>
> string '10.10.10.46' (length=11)
>
> array
> empty
>
> string
> 'ini_array['production']['agis_core']['params']['username']'
> (length=58)
>
> string 'USERNAME' (length=7)
>
> array
> empty
>
> ...
>
FYI, if I swap out the guts with this snippet:
foreach ($key_vals as $k => $v)
{
$path = 'ini_array[\''.$heading.'\']';
$subsection = explode('.', $k);
foreach ($subsection as $ss) $path .= '[\''.$ss.'\']';
$path = '$'.$path.' = \''.$v.'\';';
eval($path); //it's not elegant but it works!
}
It works. Not sure if it's any better or worse than the other hack though.
I'd still like to know what is wrong with the initial version for future
reference.
--- End Message ---