php-general Digest 6 Jun 2008 19:50:13 -0000 Issue 5500
Topics (messages 275094 through 275106):
Method chaining off constructors
275094 by: Christoph Boget
275095 by: Boyd, Todd M.
275096 by: Christoph Boget
275097 by: Eric Butera
275098 by: Adam Richardson
275099 by: Christoph Boget
275100 by: Robert Cummings
spaces - not sure if this is a preg_match issue or a regexp issue
275101 by: DeadTOm
275104 by: Jim Lucas
Re: Array of PDO objects
275102 by: Nathan Nobbe
Re: Updating inherited attributes without __construct()
275103 by: Nathan Nobbe
275105 by: Tyson Vanover
Re: Sr. PHP Engineer job opportunity / Denver
275106 by: Manuel Lemos
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 ---
Is there a reason why you can't do method chaining off of constructors?
Consider the following class:
class bob
{
public function __construct()
{
echo 'Constructor()';
}
public function one()
{
echo '->one()';
return $this;
}
public function two()
{
echo '->two()';
return $this;
}
}
This works:
$bob = new bob();
$bob->one()->two();
whereas this doesn't.
$bob = new bob()->one()->two();
Why? I thought constructors returned the object?
thnx,
Christoph
--- End Message ---
--- Begin Message ---
> -----Original Message-----
> From: Christoph Boget [mailto:[EMAIL PROTECTED]
> Sent: Friday, June 06, 2008 9:45 AM
> To: PHP General
> Subject: [PHP] Method chaining off constructors
>
> Is there a reason why you can't do method chaining off of
constructors?
>
> Consider the following class:
>
> class bob
> {
> public function __construct()
> {
> echo 'Constructor()';
> }
>
> public function one()
> {
> echo '->one()';
> return $this;
> }
>
> public function two()
> {
> echo '->two()';
> return $this;
> }
> }
>
> This works:
>
> $bob = new bob();
> $bob->one()->two();
>
> whereas this doesn't.
>
> $bob = new bob()->one()->two();
>
> Why? I thought constructors returned the object?
It's been a while since I've played with objects in PHP, but couldn't
you just add the line:
return $this;
...to the end of your __construct() function? Sorry if this is obtuse of
me to say, I just thought maybe the answer was that simple and you're
like I am--you've been staring at a tree for so long, racking your
brain, that you forget about the forest altogether. :)
Todd Boyd
Web Programmer
--- End Message ---
--- Begin Message ---
> > Why? I thought constructors returned the object?
> It's been a while since I've played with objects in PHP, but couldn't
> you just add the line:
> return $this;
> ...to the end of your __construct() function? Sorry if this is obtuse of
> me to say, I just thought maybe the answer was that simple and you're
> like I am--you've been staring at a tree for so long, racking your
> brain, that you forget about the forest altogether. :)
The constructor should already be returning $this.
thnx,
Christoph
--- End Message ---
--- Begin Message ---
On Fri, Jun 6, 2008 at 11:00 AM, Christoph Boget <[EMAIL PROTECTED]> wrote:
>> > Why? I thought constructors returned the object?
>> It's been a while since I've played with objects in PHP, but couldn't
>> you just add the line:
>> return $this;
>> ...to the end of your __construct() function? Sorry if this is obtuse of
>> me to say, I just thought maybe the answer was that simple and you're
>> like I am--you've been staring at a tree for so long, racking your
>> brain, that you forget about the forest altogether. :)
>
> The constructor should already be returning $this.
>
> thnx,
> Christoph
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
If you want to do this you need to define a function/method that
returns an instance for you. So you can say
class bob {
public static function getInstance() {
return new bob();
}
public function foo() {
}
}
bob::getInstance()->foo();
I've seen on the internals list where core dev's said it doesn't make
sense to chain off a constructor. If you want this behavior then you
need to do it off a method. This is just how things work is all it
boils down to.
--- End Message ---
--- Begin Message ---
The 'new' keyword has to apply to the object created in the constructor (and
not the return value of any of the follow-up calls.) To establish this
precedence, chaining wasn't allowed on constructors.
On Fri, Jun 6, 2008 at 11:04 AM, Eric Butera <[EMAIL PROTECTED]> wrote:
> On Fri, Jun 6, 2008 at 11:00 AM, Christoph Boget <[EMAIL PROTECTED]>
> wrote:
> >> > Why? I thought constructors returned the object?
> >> It's been a while since I've played with objects in PHP, but couldn't
> >> you just add the line:
> >> return $this;
> >> ...to the end of your __construct() function? Sorry if this is obtuse of
> >> me to say, I just thought maybe the answer was that simple and you're
> >> like I am--you've been staring at a tree for so long, racking your
> >> brain, that you forget about the forest altogether. :)
> >
> > The constructor should already be returning $this.
> >
> > thnx,
> > Christoph
> >
> >
> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
> If you want to do this you need to define a function/method that
> returns an instance for you. So you can say
>
> class bob {
>
> public static function getInstance() {
> return new bob();
> }
>
> public function foo() {
> }
>
> }
>
> bob::getInstance()->foo();
>
> I've seen on the internals list where core dev's said it doesn't make
> sense to chain off a constructor. If you want this behavior then you
> need to do it off a method. This is just how things work is all it
> boils down to.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--- End Message ---
--- Begin Message ---
> The 'new' keyword has to apply to the object created in the constructor (and
> not the return value of any of the follow-up calls.) To establish this
> precedence, chaining wasn't allowed on constructors.
If precedence was the issue, why doesn't this work, either:
(new bob())->one()->two()
? Or, rather, why couldn't that have been taken into consideration?
thnx,
Christoph
--- End Message ---
--- Begin Message ---
On Fri, 2008-06-06 at 11:35 -0400, Christoph Boget wrote:
> > The 'new' keyword has to apply to the object created in the constructor (and
> > not the return value of any of the follow-up calls.) To establish this
> > precedence, chaining wasn't allowed on constructors.
>
> If precedence was the issue, why doesn't this work, either:
>
> (new bob())->one()->two()
>
> ? Or, rather, why couldn't that have been taken into consideration?
It seems a bit futile asking on PHP General when this a question for the
PHP Internals list. That is where these kinds of decisions are made.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--- End Message ---
--- Begin Message ---
I'm working on a script to look for a UUEncoded attachment in an NNTP
message. I'm running into problems with spaces in the filename of the
attachment.
UUEncoded files in the body of a message will start with the word "begin",
then the size of the file, then the name of the file. Then the encoded
file, finaly then a newline with only the word "end" like so:
begin 644 photo.jpg
-encoded image-
end
The script looks for that first line in that order so as not to confuse it
with the word "begin" showing up somewhere else in the message. Here is
the particular line of code that searches for that:
if (preg_match("/^begin\s+[0-9][0-9][0-9]\s+(.+?)\s*\r?\n/m", $body))
The problem I'm running into is with spaces in the file name. For example,
if it starts with this:
begin 644 a_nice_photo.jpg
it works just fine, decodes the image and places it below the text of the
message. But if the line looks like this:
begin 644 a nice photo.jpg
with spaces in the filename, the script seems to stop looking after the
"a", thinks this is just normal text in the message and doesn't decode the
image. This results in the raw UUEncoded text showing up where the image
should.
I'm stumped. Any ideas?
-Allen
--
DeadTOm
http://www.mtlaners.org
[EMAIL PROTECTED]
A Linux user since 1999.
--- End Message ---
--- Begin Message ---
DeadTOm wrote:
I'm working on a script to look for a UUEncoded attachment in an NNTP
message. I'm running into problems with spaces in the filename of the
attachment.
UUEncoded files in the body of a message will start with the word "begin",
then the size of the file, then the name of the file. Then the encoded
file, finaly then a newline with only the word "end" like so:
begin 644 photo.jpg
-encoded image-
end
The script looks for that first line in that order so as not to confuse it
with the word "begin" showing up somewhere else in the message. Here is
the particular line of code that searches for that:
if (preg_match("/^begin\s+[0-9][0-9][0-9]\s+(.+?)\s*\r?\n/m", $body))
The following seems to work for me.
<?php
$body = "
begin 644 photo.jpg
-encoded image-
end
";
if ( preg_match("/begin\s+[0-9]+\s+.+/", $body, $matches) ) {
echo "found my pattern";
print_r($matches);
}
?>
Is this inside a loop that you are scanning each line of the message? If not, I
am guessing that it was your '^' that told the regex to match from the beginning
of the string.
The problem I'm running into is with spaces in the file name. For example,
if it starts with this:
begin 644 a_nice_photo.jpg
it works just fine, decodes the image and places it below the text of the
message. But if the line looks like this:
begin 644 a nice photo.jpg
with spaces in the filename, the script seems to stop looking after the
"a", thinks this is just normal text in the message and doesn't decode the
image. This results in the raw UUEncoded text showing up where the image
should.
I'm stumped. Any ideas?
-Allen
--
DeadTOm
http://www.mtlaners.org
[EMAIL PROTECTED]
A Linux user since 1999.
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--- End Message ---
--- Begin Message ---
2008/6/6 Miguel J. Jiménez <[EMAIL PROTECTED]>:
> Hi, I want to know if I can set an array with PDO objects, thus:
>
> $foo = array(new PDO(...), new PDO(...));
> $oSt = $foo[0]->prepare(...);
>
> and so on... I tried that aproach and PHP is always complaining about
> using prepare() in a non-object...
i doubt it has anything to do w/ PDO. have you tried var_dump() to inspect
the contents of $foo[0] ? and also, have you tried creating the PDO
instances w/ the same arguments outside of the array construct to determine
if there is any difference?
-nathan
--- End Message ---
--- Begin Message ---
On Thu, Jun 5, 2008 at 12:15 PM, Tyson Vanover <[EMAIL PROTECTED]> wrote:
> I have a class that has a list of valid keys, and an array of values. When
> a value is added to the array it's key is first checked against the list of
> valid keys (this is to prevent injection issues we have been having later on
> in the project).
>
> class parent{
> private $validkeys = 'title,color,name';
> private $values = array();
> }
> That class is inherited by other classes that mostly just have an expanded
> list of valid keys. I would like to be able to update the valid key list
> without having to craft a constructor for child objects. I would rather not
> give each child class the constructor,
>
> __construct()
> {
> $this->validkeys.= ',setting,...';
> parent::__construct();
> }
>
> since most child classes have no need of a unique constructor.
i dont quite understand this reasoning.. if you want to add some valid keys,
based upon the design of the parent, it seems to me the children do have a
need for an overridden constructor. if you have accessor methods w/ a
consistent naming convention, you could omit the $validKeys array
altogether, and determine if a given field is valid based upon runtime
introspection. something like this,
/// in the parent class
protected function isFieldValid($field) {
if(method_exists($this, 'get' . ucfirst($field)))
return true;
else
return false;
}
this will work in all the child classes as well, as long as you stick to a
consistent naming convention, and you can of course add a check for a set..
method as well if you like. im not saying this technique is the holy grail
or anything, im merely offering it as an alternative to your current
solution.
-nathan
--- End Message ---
--- Begin Message ---
Sorry, I probably should have included the add()
function from the parent. As well as all of the parent
constructor.
Basically the object takes an array of key=>value pairs
and parses them into a string for output. When it
takes in the array of pairs it needs to check the keys
against a list of valid keys. I have no preference on
how to store the list of valid keys other than to add
more a constructor doesn't need to be called.
class parent{
$validkeys = 'name,size';
$attributes = array();
__construct($set){
foreach($set as $key=>$value){
if(isValidKey($key)){
$this->attributes[$key] = $value;
}
}
}
__toString(){
$output = "";
foreach($this->attributes as $key=>value){
$output .= sprintf(TEMPLATE, $key, $value);
}
}
__set($key, $value){
if(isValidKey($key)){
$this->attributes[$key] = $value;
}
}
isValidKey($key){
...Something goes here...
}
}
class child extends parent{
...All this needs to do is tack on values 'color,shape'
to parent's valid keys...
}
class grandchild extends child{
...All this needs to do is tack on values
'cost,location' to child's valid keys...
}
Most sub classes won't need anything different from
their parent but the expanded list of valid keys.
Nathan Nobbe wrote:
i dont quite understand this reasoning.. if you want to add some valid keys,
based upon the design of the parent, it seems to me the children do have a
need for an overridden constructor. if you have accessor methods w/ a
consistent naming convention, you could omit the $validKeys array
altogether, and determine if a given field is valid based upon runtime
introspection. something like this,
/// in the parent class
protected function isFieldValid($field) {
if(method_exists($this, 'get' . ucfirst($field)))
return true;
else
return false;
}
this will work in all the child classes as well, as long as you stick to a
consistent naming convention, and you can of course add a check for a set..
method as well if you like. im not saying this technique is the holy grail
or anything, im merely offering it as an alternative to your current
solution.
-nathan
> On Thu, Jun 5, 2008 at 12:15 PM, Tyson Vanover
<[EMAIL PROTECTED]> wrote:
>
>> I have a class that has a list of valid keys, and
an array of values. When
>> a value is added to the array it's key is first
checked against the list of
>> valid keys (this is to prevent injection issues we
have been having later on
>> in the project).
>>
>> class parent{
>> private $validkeys = 'title,color,name';
>> private $values = array();
>> }
>> That class is inherited by other classes that
mostly just have an expanded
>> list of valid keys. I would like to be able to
update the valid key list
>> without having to craft a constructor for child
objects. I would rather not
>> give each child class the constructor,
>>
>> __construct()
>> {
>> $this->validkeys.= ',setting,...';
>> parent::__construct();
>> }
>>
>> since most child classes have no need of a unique
constructor.
--- End Message ---
--- Begin Message ---
Hello,
> I have an immediate opportunity available for a Senior Software Engineer in
> Denver, CO (relocation assistance is available). This is a great opportunity
> to join a dynamic and growing Internet-based company. The individual will
> be responsible for the development, implementation, and maintenance of our
> scalable, reusable, web/software based user interfaces. You must be familiar
> with design patterns and be able to write abstract classes that adhere to
> standard OOP methodologies. The qualified candidate will expand our
> web-based platform, building new features and products.
You may want to take a look at this directory of PHP professionals
available for taking PHP jobs in the United States. You may even narrow
your search professionals that have the skills you need:
http://www.phpclasses.org/professionals/country/us/
--
Regards,
Manuel Lemos
PHP professionals looking for PHP jobs
http://www.phpclasses.org/professionals/
PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/
--- End Message ---