php-general Digest 15 Dec 2011 13:58:38 -0000 Issue 7611
Topics (messages 316019 through 316026):
Re: Preferred Syntax
316019 by: Rick Dwyer
316023 by: Ross McKay
316026 by: Robert Cummings
Re: How to use a variable variable with an array
316020 by: Nils Leideck
316021 by: Robert Cummings
Re: How to use a variable variable with an array [solved]
316022 by: Nils Leideck
OOP problems
316024 by: Dominik Halvoník
316025 by: Alex Pojarsky
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
On Dec 14, 2011, at 1:53 PM, <ad...@buskirkgraphics.com> <ad...@buskirkgraphics.com
> wrote:
The key thing to remember here is that this is a preference and not a
performance thing.
Thank you... this is basically what I wanted to know. I was concerned
that not breaking the VARS out separately from the echo'ed text might
cause some sort of performance problem or confusion for PHP.
Therefore, whenever I came across them, I was breaking them out with
the . $var . technique. Will no longer do that for existing code, as
it appears not necessary, but I do prefer it for readability sake...
expecially in BBEdit. So will continue to write new code breaking it
out. And I too prefer a single quote for PHP and a double for HTML...
even though the sample I displayed showed otherwise.
Thanks to all who responded.
--Rick
--- End Message ---
--- Begin Message ---
On Wed, 14 Dec 2011 07:59:46 -0500, Rick Dwyer wrote:
>Can someone tell me which of the following is preferred and why?
>
> echo "<a style='text-align:left;size:14;font-weight:bold' href='/
>mypage.php/$page_id'>$page_name</a><br>";
>
> echo "<a style='text-align:left;size:14;font-weight:bold' href='/
>mypage.php/".$page_id."'>".$page_name."</a><br>";
>[...]
Just to throw in yet another possibility:
echo <<<HTML
<a style="text-align:left;size:14;font-weight:bold"
href="/mypage.php/$page_id">$page_name</a><br>
HTML;
I love HEREDOC for slabs of HTML, sometimes SQL, email bodies, etc.
because they allow you to drop your variables into the output text
without crufting up the formatting with string concatenation, AND they
allow you to use double quotes which can be important for HTML
attributes that may contain single quotes.
So whilst either above option is fine for the specific context, I prefer
HEREDOC when there's attributes like href.
But what is "preferred" is rather dependent on the "preferrer".
--
Ross McKay, Toronto NSW Australia
"All we are saying
Is give peas a chance" - SeedSavers
--- End Message ---
--- Begin Message ---
On 11-12-15 02:50 AM, Ross McKay wrote:
On Wed, 14 Dec 2011 07:59:46 -0500, Rick Dwyer wrote:
Can someone tell me which of the following is preferred and why?
echo "<a style='text-align:left;size:14;font-weight:bold' href='/
mypage.php/$page_id'>$page_name</a><br>";
echo "<a style='text-align:left;size:14;font-weight:bold' href='/
mypage.php/".$page_id."'>".$page_name."</a><br>";
[...]
Just to throw in yet another possibility:
echo<<<HTML
<a style="text-align:left;size:14;font-weight:bold"
href="/mypage.php/$page_id">$page_name</a><br>
HTML;
I love HEREDOC for slabs of HTML, sometimes SQL, email bodies, etc.
because they allow you to drop your variables into the output text
without crufting up the formatting with string concatenation, AND they
allow you to use double quotes which can be important for HTML
attributes that may contain single quotes.
So whilst either above option is fine for the specific context, I prefer
HEREDOC when there's attributes like href.
But what is "preferred" is rather dependent on the "preferrer".
Heredoc and Nowdoc are nice but I hate the way they muck up my
indentation aesthetics. As such when I use them I use as minimalist a
terminator as possible:
<?php
echo <<<_
<a href="foo.html">Blah blah blah</a>
_;
?>
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 ---
Hi FeIn and Laruence,
On 14.12.2011, at 09:47, Nils Leideck wrote:
>> function coolFunction( array $array, $search, $separator = ‘###' )
>> {
> [...]
>> if ( isset( $array[$dimension] ) ) {
>> // no more dimensions to search
>> if ( empty( $dimensions ) ) {
>> return $array[$dimension];
>> }
>> // more dimensions to search but we can only go further if
>> // the current dimension value is also an array
>> if ( is_array( $array[$dimension] ) ) {
>> return coolFunction( $array[$dimension], $dimensions[0] );
> [...]
>
> That looks very promising! Great feedback!
> I will try this today and feedback.
The function above works pretty well for my needs, many thanks for your
valuable feedback!
Cheers, Nils
--
http://webint.cryptonode.de / a Fractal project
--- End Message ---
--- Begin Message ---
On 11-12-14 01:11 AM, Laruence wrote:
On Wed, Dec 14, 2011 at 9:27 AM, Nils Leideck<nils.leid...@leidex.net> wrote:
Hi Al,
many thanks for your feedback. Unfortunately I don’t know the deepness of the
array so I can’t use the nested foreach() idea :-( Let me try to simplify my
question (which also helps myself to clarify my process ;-)
I have a variable where the value is a string that is exactly the path to the
array value:
[code]
$arrayPath = "['user_interface’][‘design']['my_colors']['item_number_one’]”;
[/code]
And I have an array that has all the details:
[code]
$myArray = coolFunction(‘getArray’);
[/code]
The question is, how can I use these both informations to return a value from
the $myArray?
Examples I tried unsuccessfully:
[code]
[...]
echo $myArray$arrayPath;
echo $myArray{$arrayPath};
echo $myArray${arrayPath};
echo $myArray${$arrayPath};
echo $myArray.$arrayPath;
echo $myArray.$arrayPath;
[...]
[/code]
etc...
your feedback is much much much appreciated!!!
Cheers, Nils
--
http://webint.cryptonode.de / a Fractal project
for you quesion, try eval:
<?php
$arr = array("a"=>array("b"=>array("c")));
$key = "['a']'['b]";
$value = eval("return \$arr". $key .";");
?>
ps: your requirement is a little odd :)
If the solution is to use eval() then you've almost certainly come up
with the wrong answer.
To get the desired answer you can either use recursion to recursively
index the array as necessitated by the "path" or you can use references
to incremententally step through the array. Example:
<?php
$path = 'path/to/data/in/my/arbitrarily/deep/array';
$array = $someBigFatArray;
$focus = &$array();
foreach( explode( '/', $path ) as $step )
{
if( isset( $focus[$step] ) )
{
$focus = &$focus[$step];
}
else
{
unset( $focus );
$focus = null;
break;
}
}
echo $focus;
?>
You can wrap this puppy in a function too... and it will be faster than
the recursive version someone else wrote because it doesn't incur the
overhead of multiple function calls (for depths greater than 1).
Recursion is great, recursion is elegant, computer science professors
love recursion, but it can also be a resource pig and PHP doesn't do
tail recursion :)
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 ---
Dear Robert,
On 14.12.2011, at 22:02, Robert Cummings wrote:
> $path = ‘/path/to/data/in/my/arbitrarily/deep/array/';
> $array = $someBigFatArray;
> $focus = &$array;
> foreach( explode( '/', $path ) as $step )
> {
> if ($step === '' || $step === 'CONFIGURATION’)
> {
> continue;
> }
> if( isset( $focus[$step] ) )
> {
> $focus = &$focus[$step];
> }
> else
> {
> unset( $focus );
> $focus = null;
> break;
> }
> }
> echo "<pre>"; echo var_export($focus, TRUE); echo "</pre>";
This solution is excellent! I modified it a little to match for empty $step and
then continue, just in case there is a leading, ending or doubled “/“ and same
for CONFIGURATION which will be my indicator to find the values where we expect
a value replacement.
if ($step === '' || $step === 'CONFIGURATION’) {
continue;
}
Great thing!
Thanks a lot to all!
Cheers, Nils
--
http://webint.cryptonode.de / a Fractal project
--- End Message ---
--- Begin Message ---
Hello,
I would like to ask you for help. This days I am trying to build one of my
applications. But I have problem which stopped me. I have folder whit php
files like connect.php, delete.php etc. These files contains classes named
the same as files. So in file connect.php is class Connect. These files are
placed in folder named mysql and this folder is inside folder named db. In
folder db is a php file named mysql.php, in this file I include classes
from folder mysql, after include I declare class MySQL and in it I have
method __construct(). In this method I create dynamic objects from included
classes. And this is the problem that I can not solve, I have more then one
of this files(mysql.php[whit class MySQL], oracle.php[whit class Oracle]
etc.) and I need to include them to file called db.php that is in the main
folder of my app. In db.php is an class called db, how can I add classes
MySQL, Oracle etc. to class db? I try to use abstract class whit __set and
__get methods but I also need to include class db to main class
application. I am really sorry for my English, so please be indulgent. So I
need to connect classes like this:
application->db->mysql->connect, but I can not use extends because in php
you can have only one parent class. The reason why I am trying to do
something like this is because I want to call methods like this:
$test = new application();
$test->db->connect();
If it is mysql or othet database I set in config.php file.
I need to achieve this schema( -> is something like ../ it means that it is
one level up folder):
connec.php(class Connect MySql)->
select.php(class Select MySql) ->
.... -> mysql.php(class MySQL include all classes, Connect...)->
.... ->
... ->
-> db.php(class db include all classes, MySQL, Oracle..)
connec.php(class Connect Oracle)->
select.php(class Select Oracle ) ->
.... -> oracle .php(class Oracle include all classes, Connect...)->
.... ->
... ->
download.php(class Download)->
unzip.php(class Unzip) ->
.... -> files.php(class Files include all classes, Download...) ->
file.php(class file include class Files)
.... ->
... ->
hash.php(class Hash)->
capcha.php(class Capcha) ->
.... -> secure.php(class Secure include all classes, Hash...) ->
security.php(class security include class Secure)
.... ->
... ->
ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect.
And in the end, in the same folder as db.php and security.php I will have
file application.php which will contain class application and in its
__construct() method I will make link classes db, security, file ect. ect.
So I will just include file application.php make object from class
application and then just do $object->db->connect()(of course if it will by
MySql or other database will be stored in some config.php file).
Thanks,
Dominik
--- End Message ---
--- Begin Message ---
I'm not sure I've understood you correctly, but you may try something
like the following primitive autoloader (I didn't debug it, it's just
an example):
class Base
{
protected $_path = '';
public function construct($base_path)
{
$this->_path = $base_path;
}
public function __get($name)
{
$requested_path = $this->_path . DIRECTORY_SEPARATOR . $name;
if (is_dir($requested_path))
{
return new Base($requested_path);
}
else if (is_file($requested_path . '.php'))
{
include ($requested_path . '.php');
$classname = ucfirst($name);
return new $clasname();
}
}
}
// Assuming you have Mysql class in /home/user/project/classes/db/mysql.php
// you may try
$base = new Base("/home/user/project/classes/");
$base->db->mysql->someFunctionOfMysqlClass();
2011/12/15 Dominik Halvoník <dominik.halvo...@gmail.com>:
> Hello,
>
> I would like to ask you for help. This days I am trying to build one of my
> applications. But I have problem which stopped me. I have folder whit php
> files like connect.php, delete.php etc. These files contains classes named
> the same as files. So in file connect.php is class Connect. These files are
> placed in folder named mysql and this folder is inside folder named db. In
> folder db is a php file named mysql.php, in this file I include classes
> from folder mysql, after include I declare class MySQL and in it I have
> method __construct(). In this method I create dynamic objects from included
> classes. And this is the problem that I can not solve, I have more then one
> of this files(mysql.php[whit class MySQL], oracle.php[whit class Oracle]
> etc.) and I need to include them to file called db.php that is in the main
> folder of my app. In db.php is an class called db, how can I add classes
> MySQL, Oracle etc. to class db? I try to use abstract class whit __set and
> __get methods but I also need to include class db to main class
> application. I am really sorry for my English, so please be indulgent. So I
> need to connect classes like this:
>
> application->db->mysql->connect, but I can not use extends because in php
> you can have only one parent class. The reason why I am trying to do
> something like this is because I want to call methods like this:
> $test = new application();
> $test->db->connect();
>
> If it is mysql or othet database I set in config.php file.
>
> I need to achieve this schema( -> is something like ../ it means that it is
> one level up folder):
>
> connec.php(class Connect MySql)->
> select.php(class Select MySql) ->
> .... -> mysql.php(class MySQL include all classes, Connect...)->
> .... ->
> ... ->
> -> db.php(class db include all classes, MySQL, Oracle..)
> connec.php(class Connect Oracle)->
> select.php(class Select Oracle ) ->
> .... -> oracle .php(class Oracle include all classes, Connect...)->
> .... ->
> ... ->
>
> download.php(class Download)->
> unzip.php(class Unzip) ->
> .... -> files.php(class Files include all classes, Download...) ->
> file.php(class file include class Files)
> .... ->
> ... ->
>
> hash.php(class Hash)->
> capcha.php(class Capcha) ->
> .... -> secure.php(class Secure include all classes, Hash...) ->
> security.php(class security include class Secure)
> .... ->
> ... ->
> ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect. ect.
>
> And in the end, in the same folder as db.php and security.php I will have
> file application.php which will contain class application and in its
> __construct() method I will make link classes db, security, file ect. ect.
> So I will just include file application.php make object from class
> application and then just do $object->db->connect()(of course if it will by
> MySql or other database will be stored in some config.php file).
>
> Thanks,
>
> Dominik
--- End Message ---