Re: [PHP] Re: Preferred Syntax

2011-12-15 Thread Ross McKay
Jim Lucas wrote:

>I second this example, with one minor change, I would add '{' and '}' around
>variables.
>
>echo <<   href="/mypage.php/{$page_id}">{$page_name}
>HTML;
>
>This works for $variables, $objects, and variable functions calls.  But doesn't
>work if you try to call functions directly (bummer).

In fact, we are in agreement here :) I was just simplifying for the
example at hand. And as for calling functions directly, also add
constants :(

However, it's easy enough to assign a constant to a variable and embed
it in a HEREDOC, and also easy to wrap a function in a method,
especially when your HEREDOC is within a method itself:

define('MSG', 'My name is');

class X {
  function html($text) {
return htmlspecialchars($text);
  }

  function output($name) {
$msg = MSG;
echo <<$msg {$this->html($name)}
HTML;
  }
}

$x = new X();
$x->output('silly "rockstar" name like <&>');

>[...]
>This is why I like heredoc syntax over pretty much everything else.

Concur!
-- 
Ross McKay, Toronto, NSW Australia
"Pay no attention to that man behind the curtain" - Wizard of Oz

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



Re: [PHP] Preferred Syntax

2011-12-15 Thread Tedd Sperling
On Dec 14, 2011, at 12:09 PM, Peter Ford wrote:
> 
> With respect to tedd and Al, you've misread the question: the important 
> PHP-related bit is about whether to embed variables in double-quoted strings 
> or to concatenate them. These are only two of the options, and each has it's 
> pros and cons. There has been (some time ago) plenty of discussion on this 
> list about this sort of thing, including (ISTR) a timed test of the 
> performance implications of various forms - that only really matters in big 
> loops, of course...
> 
> Horses for courses. I use whatever I feel like at the time, and mix various 
> styles, as long as it's readable!
> 
> Cheers
> Pete
> 

I didn't misread the OP's post. Realize that the OP provided two ways of doing 
the task and asked which way was best?

I responded with a recommendation to remove all style elements and my 
recommended solution to his problem.

He can either consider or not.

Cheers,

tedd

_
t...@sperling.com
http://sperling.com




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



Re: [PHP] Unique items in an array

2011-12-15 Thread Marc Guay
> Give it a try, should do what you are wanting.

Hi Jim,

I appreciate your dedication to this problem but it was solved 2 days ago!  :)

Thanks
Marc

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



Re: [PHP] Unique items in an array

2011-12-15 Thread Jim Lucas
On 12/15/2011 6:24 AM, Marc Guay wrote:
>> Assuming you want to make things unique based on the "contact_first_name" 
>> field,
>> how would you decide which record to keep?  The first one you run in to, the
>> last one you come across, or some other criteria?
> 
> The unique field is actually the contact_id.
> 
> Marc
> 

Marc,

If that is the case, can you explain to us how you got the two entries in the
array to begin with?  If this is from a DB query, then it should probably be
address else where.  If it was loaded from a text file, it should be handled
differently.

No matter how it was created, you could always build a method (if the dataset
isn't too large) that would filter things out.

Given your example array, I would do something like this:

 "356",
"contact_first_name" => "Marc",
  ),
  array(
"contact_id" => "247",
"contact_first_name" => "Marc",
  ),
  array(
"contact_id" => "356",
"contact_first_name" => "Marc",
  ),
);

$newDataSet = array();

foreach ( $oldDataSet AS $k => $entry ) {
  $newDataSet[$entry['contact_id']] = $entry;
  unset($oldDataSet[$k]);
}

print_r($newDataSet);

?>

This would result in something like this:

Array
(
[356] => Array
(
[contact_id] => 356
[contact_first_name] => Marc
)

[247] => Array
(
[contact_id] => 247
[contact_first_name] => Marc
)

)

Give it a try, should do what you are wanting.

-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

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



Re: [PHP] Re: Preferred Syntax

2011-12-15 Thread Jim Lucas
On 12/14/2011 11:50 PM, 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 "$page_name";
>>
>>  echo "".$page_name."";
>> [...]
> 
> Just to throw in yet another possibility:
> 
> echo << href="/mypage.php/$page_id">$page_name
> 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".

I second this example, with one minor change, I would add '{' and '}' around
variables.

echo <<{$page_name}
HTML;

This works for $variables, $objects, and variable functions calls.  But doesn't
work if you try to call functions directly (bummer).

$ cat variable_usage.php
v1 = 'Object 1';

function something($str) {
return "...{$str}...";
}

$f1 = "something";

echo <<<_
{$v1}
{$o1->v1}
{$f1('Function 1')}
{something('F1')}
_;

?>

Results in this

$ php -f variable_usage.php
Variable 1
Object 1
...Function 1...
{something('F1')}


This is why I like heredoc syntax over pretty much everything else.
-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

C - (541) 408-5189
O - (541) 323-9113
H - (541) 323-4219

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



Re: [PHP] OOP problems

2011-12-15 Thread Fatih P.


On 12/15/2011 01:05 PM, Alex Pojarsky wrote:

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:

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

Why don't you modify include_path on initialization of 'Base' class?

would make things much simpler.

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



Re: [PHP] Unique items in an array

2011-12-15 Thread Marc Guay
> Assuming you want to make things unique based on the "contact_first_name" 
> field,
> how would you decide which record to keep?  The first one you run in to, the
> last one you come across, or some other criteria?

The unique field is actually the contact_id.

Marc

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



Re: [PHP] Re: Preferred Syntax

2011-12-15 Thread Louis Huppenbauer
Another nice way would be sprintf. So your string really is just a string
and nothing more.
I don't know how it would affect performance, but just for the eye I find
it much simpler.

echo sprintf("%s", $page_id, $page_name);

2011/12/15 Robert Cummings 

> 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 "$page_**name";
>>>
>>>  echo "".$**page_name."";
>>> [...]
>>>
>>
>> Just to throw in yet another possibility:
>>
>> echo<<> >href="/mypage.php/$page_id">$**page_name
>> 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:
>
> 
>echo <<<_
>Blah blah blah
> _;
>
> ?>
>
>
> 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.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] Re: Preferred Syntax

2011-12-15 Thread Robert Cummings

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 "$page_name";

  echo "".$page_name."";
[...]


Just to throw in yet another possibility:

echo<<$page_name
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:


Blah blah blah
_;

?>

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.

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



Re: [PHP] OOP problems

2011-12-15 Thread Alex Pojarsky
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 :
> 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

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



[PHP] OOP problems

2011-12-15 Thread Dominik Halvoník
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