How do YOU set default function/method params?

How do YOU set default function/method params?

am 06.10.2009 02:48:32 von List Manager

Here is a problem that I have had for years now. I have been trying to come up
with the perfect solution for this problem. But, I have come down to two
different methods for solving it.

Here is the problem...


function sendEmail(
$to,
$from,
$subject,
$body,
$attachments=array(),
$headers=array()
) { # I typically do not put each argument on seperate lines, but I ran
#out of width in this email...

# do something here...
mail(...);

}

sendEmail('john@doe.com',
'marykate@uhhh.net',
'Hi!',
'Check out my new pictures!!!',
$hash_array_of_pictures
);

Now, we all have a function or method like this floating around somewhere.

My question is, how do YOU go about setting the required entries of the $headers
array() ?

I see three possible solutions. I want to see a clean and simple solution.

Here are my ideas so far:

function sendEmail(
$to,
$from,
$subject,
$body,
$attachments=array(),
$headers=array()
) { # I typically do not put each argument on seperate lines, but I ran
#out of width in this email...

if ( empty($headers['Date']) ) {
$headers['Date'] = date('c');
}
if ( empty($headers['Message-ID']) ) {
$headers['Date'] = md5($to.$subject);
}
# and the example goes on...

# do something here...
mail(...);

}

Or, another example. (I will keep it to the guts of the solution now)

$headers['Date'] = empty($headers['Date']) ?
date('c') : $headers['Date'];
$headers['Message-ID'] = empty($headers['Message-ID']) ?
md5($to.$subject) : $headers['Message-ID'];

OR, yet another example...

$defaults = array(
'Date' => date('c'),
'Message-ID' => md5($to.$subject),
);

$headers += $defaults;

END of examples...

Now, IMO, the last one is the simplest one and for me, I think it will be the
new way that I solve this type of problem.

But, my question that I put out to all of you is...

How would you solve this problem?

TIA

Jim Lucas

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

Re: How do YOU set default function/method params?

am 06.10.2009 02:54:09 von Eddie Drapkin

On Mon, Oct 5, 2009 at 8:48 PM, Jim Lucas wrote:
> Here is a problem that I have had for years now.  I have been trying=
to come up
> with the perfect solution for this problem.  But, I have come down t=
o two
> different methods for solving it.
>
> Here is the problem...
>
> >
> function sendEmail(
>    $to,
>    $from,
>    $subject,
>    $body,
>    $attachments=3Darray(),
>    $headers=3Darray()
>    ) { # I typically do not put each argument on seperate lines=
, but I ran
>        #out of width in this email...
>
>    # do something here...
>    mail(...);
>
> }
>
> sendEmail('john@doe.com',
>    'marykate@uhhh.net',
>    'Hi!',
>    'Check out my new pictures!!!',
>    $hash_array_of_pictures
>    );
>
> Now, we all have a function or method like this floating around somewhere=
..
>
> My question is, how do YOU go about setting the required entries of the $=
headers
> array() ?
>
> I see three possible solutions.  I want to see a clean and simple so=
lution.
>
> Here are my ideas so far:
>
> function sendEmail(
>    $to,
>    $from,
>    $subject,
>    $body,
>    $attachments=3Darray(),
>    $headers=3Darray()
>    ) { # I typically do not put each argument on seperate lines=
, but I ran
>        #out of width in this email...
>
>    if ( empty($headers['Date']) ) {
>        $headers['Date'] =3D date('c');
>    }
>    if ( empty($headers['Message-ID']) ) {
>        $headers['Date'] =3D md5($to.$subject);
>    }
>    # and the example goes on...
>
>    # do something here...
>    mail(...);
>
> }
>
> Or, another example.  (I will keep it to the guts of the solution no=
w)
>
>    $headers['Date']       =3D empty($headers['Da=
te']) ?
>                     =C2=
=A0       date('c') : $headers['Date'];
>    $headers['Message-ID'] =3D empty($headers['Message-ID']) ?
>                     =C2=
=A0       md5($to.$subject) : $headers['Message-ID'];
>
> OR, yet another example...
>
> $defaults =3D array(
>    'Date'       =3D> date('c'),
>    'Message-ID' =3D> md5($to.$subject),
> );
>
> $headers +=3D $defaults;
>
> END of examples...
>
> Now, IMO, the last one is the simplest one and for me, I think it will be=
the
> new way that I solve this type of problem.
>
> But, my question that I put out to all of you is...
>
>        How would you solve this problem?
>
> TIA
>
> Jim Lucas
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

How does this look to you?

function sendEmail(
$to,
$from,
$subject,
$body,
$attachments=3Darray(),
$headers=3Darray()
) {
# I typically do not put each argument on seperate lines, but I ran
#out of width in this email...

$default_headers =3D array(
'Date' =3D> date('c'),
'Message-ID' =3D> md5($to.$subject)
);

$headers =3D array_merge($default_headers, $headers);
# and the example goes on...

# do something here...
mail(...);

}

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

Re: How do YOU set default function/method params?

am 06.10.2009 03:20:41 von List Manager

Eddie Drapkin wrote:
> On Mon, Oct 5, 2009 at 8:48 PM, Jim Lucas wrote:
>> Here is a problem that I have had for years now. I have been trying to come up
>> with the perfect solution for this problem. But, I have come down to two
>> different methods for solving it.
>>
>> Here is the problem...
>>
>> >>
>> function sendEmail(
>> $to,
>> $from,
>> $subject,
>> $body,
>> $attachments=array(),
>> $headers=array()
>> ) { # I typically do not put each argument on seperate lines, but I ran
>> #out of width in this email...
>>
>> # do something here...
>> mail(...);
>>
>> }
>>
>> sendEmail('john@doe.com',
>> 'marykate@uhhh.net',
>> 'Hi!',
>> 'Check out my new pictures!!!',
>> $hash_array_of_pictures
>> );
>>
>> Now, we all have a function or method like this floating around somewhere.
>>
>> My question is, how do YOU go about setting the required entries of the $headers
>> array() ?
>>
>> I see three possible solutions. I want to see a clean and simple solution.
>>
>> Here are my ideas so far:
>>
>> function sendEmail(
>> $to,
>> $from,
>> $subject,
>> $body,
>> $attachments=array(),
>> $headers=array()
>> ) { # I typically do not put each argument on seperate lines, but I ran
>> #out of width in this email...
>>
>> if ( empty($headers['Date']) ) {
>> $headers['Date'] = date('c');
>> }
>> if ( empty($headers['Message-ID']) ) {
>> $headers['Date'] = md5($to.$subject);
>> }
>> # and the example goes on...
>>
>> # do something here...
>> mail(...);
>>
>> }
>>
>> Or, another example. (I will keep it to the guts of the solution now)
>>
>> $headers['Date'] = empty($headers['Date']) ?
>> date('c') : $headers['Date'];
>> $headers['Message-ID'] = empty($headers['Message-ID']) ?
>> md5($to.$subject) : $headers['Message-ID'];
>>
>> OR, yet another example...
>>
>> $defaults = array(
>> 'Date' => date('c'),
>> 'Message-ID' => md5($to.$subject),
>> );
>>
>> $headers += $defaults;
>>
>> END of examples...
>>
>> Now, IMO, the last one is the simplest one and for me, I think it will be the
>> new way that I solve this type of problem.
>>
>> But, my question that I put out to all of you is...
>>
>> How would you solve this problem?
>>
>> TIA
>>
>> Jim Lucas
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> How does this look to you?
>
> function sendEmail(
> $to,
> $from,
> $subject,
> $body,
> $attachments=array(),
> $headers=array()
> ) {
> # I typically do not put each argument on seperate lines, but I ran
> #out of width in this email...
>
> $default_headers = array(
> 'Date' => date('c'),
> 'Message-ID' => md5($to.$subject)
> );
>
> $headers = array_merge($default_headers, $headers);
> # and the example goes on...
>
> # do something here...
> mail(...);
>
> }
>

Good, since it is a combination of the examples I gave.

I am looking at how you would solve the problem. Unless this is the way you would solve the
problem.. :-D

Jim

--
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

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

Re: How do YOU set default function/method params?

am 06.10.2009 07:56:50 von paragasu

why bother, i use available good library

http://swiftmailer.org/



On 10/6/09, Jim Lucas wrote:
> Eddie Drapkin wrote:
>> On Mon, Oct 5, 2009 at 8:48 PM, Jim Lucas wrote:
>>> Here is a problem that I have had for years now. I have been trying to
>>> come up
>>> with the perfect solution for this problem. But, I have come down to two
>>> different methods for solving it.
>>>
>>> Here is the problem...
>>>
>>> >>>
>>> function sendEmail(
>>> $to,
>>> $from,
>>> $subject,
>>> $body,
>>> $attachments=array(),
>>> $headers=array()
>>> ) { # I typically do not put each argument on seperate lines, but I
>>> ran
>>> #out of width in this email...
>>>
>>> # do something here...
>>> mail(...);
>>>
>>> }
>>>
>>> sendEmail('john@doe.com',
>>> 'marykate@uhhh.net',
>>> 'Hi!',
>>> 'Check out my new pictures!!!',
>>> $hash_array_of_pictures
>>> );
>>>
>>> Now, we all have a function or method like this floating around
>>> somewhere.
>>>
>>> My question is, how do YOU go about setting the required entries of the
>>> $headers
>>> array() ?
>>>
>>> I see three possible solutions. I want to see a clean and simple
>>> solution.
>>>
>>> Here are my ideas so far:
>>>
>>> function sendEmail(
>>> $to,
>>> $from,
>>> $subject,
>>> $body,
>>> $attachments=array(),
>>> $headers=array()
>>> ) { # I typically do not put each argument on seperate lines, but I
>>> ran
>>> #out of width in this email...
>>>
>>> if ( empty($headers['Date']) ) {
>>> $headers['Date'] = date('c');
>>> }
>>> if ( empty($headers['Message-ID']) ) {
>>> $headers['Date'] = md5($to.$subject);
>>> }
>>> # and the example goes on...
>>>
>>> # do something here...
>>> mail(...);
>>>
>>> }
>>>
>>> Or, another example. (I will keep it to the guts of the solution now)
>>>
>>> $headers['Date'] = empty($headers['Date']) ?
>>> date('c') : $headers['Date'];
>>> $headers['Message-ID'] = empty($headers['Message-ID']) ?
>>> md5($to.$subject) : $headers['Message-ID'];
>>>
>>> OR, yet another example...
>>>
>>> $defaults = array(
>>> 'Date' => date('c'),
>>> 'Message-ID' => md5($to.$subject),
>>> );
>>>
>>> $headers += $defaults;
>>>
>>> END of examples...
>>>
>>> Now, IMO, the last one is the simplest one and for me, I think it will be
>>> the
>>> new way that I solve this type of problem.
>>>
>>> But, my question that I put out to all of you is...
>>>
>>> How would you solve this problem?
>>>
>>> TIA
>>>
>>> Jim Lucas
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>
>> How does this look to you?
>>
>> function sendEmail(
>> $to,
>> $from,
>> $subject,
>> $body,
>> $attachments=array(),
>> $headers=array()
>> ) {
>> # I typically do not put each argument on seperate lines, but I ran
>> #out of width in this email...
>>
>> $default_headers = array(
>> 'Date' => date('c'),
>> 'Message-ID' => md5($to.$subject)
>> );
>>
>> $headers = array_merge($default_headers, $headers);
>> # and the example goes on...
>>
>> # do something here...
>> mail(...);
>>
>> }
>>
>
> Good, since it is a combination of the examples I gave.
>
> I am looking at how you would solve the problem. Unless this is the way you
> would solve the
> problem.. :-D
>
> Jim
>
> --
> 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
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

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

RE: How do YOU set default function/method params?

am 06.10.2009 08:01:50 von Mert Oztekin

--_000_E2C046087E10D943811A0BD0A4E8316D1B57CD1356ankaraanado lu_
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

SU1PLCBhcnJheV9tZXJnZSgpIGlzIGVhc3kgdG8gdXNlLCBob3dldmVyIGl0 IHN1cHBvc2UgdG8g
dXNlIG1vcmUgY3B1IHRoYW4gb3RoZXIgb3B0aW9ucy4gSWYgeW91IGFyZSBh IHBlcmZvcm1hbmNl
IGZyZWFrLCBpIHN1Z2dlc3QgeW91IHRvIG5vdCBjaG9vc2UgYXJyYXlfbWVy Z2UoKSBzb2x1dGlv
bi4gKGFsc28geW91IGV4ZWN1dGUgZGF0ZSgpIGFuZCBtZDUoKSBmdW5jdGlv bnMgZXZlbiBpZiB5
b3Ugd29udCBuZWVkIHRvIHVzZSB0aGVtKQ0KDQpJbiB0aGUgb3RoZXIgd2F5 LCBJIHRoaW5rIGZp
cnN0IG9wdGlvbiAod2l0aCBpZigpIG9uZXMpIHNlZW1zIG1vcmUgcmVhZGFi bGUgdGhhbiBvdGhl
ciBvbmVzLg0KDQotLS0tLU9yaWdpbmFsIE1lc3NhZ2UtLS0tLQ0KRnJvbTog SmltIEx1Y2FzIFtt
YWlsdG86bGlzdHNAY21zd3MuY29tXQ0KU2VudDogVHVlc2RheSwgT2N0b2Jl ciAwNiwgMjAwOSA0
OjIxIEFNDQpUbzogRWRkaWUgRHJhcGtpbg0KQ2M6IHBocCBHZW5lcmFsIExp c3QNClN1YmplY3Q6
IFJlOiBbUEhQXSBIb3cgZG8gWU9VIHNldCBkZWZhd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W5nZXMgb3Ig
bGF0ZSByZWNlaXZpbmcsIGxvc3Mgb2YgaW50ZWdyaXR5IGFuZCBjb25maWRl bnRpYWxpdHksIHZp
cnVzZXMgYW5kIGFueSBkYW1hZ2VzIGNhdXNlZCBpbiBhbnl3YXkgdG8geW91 ciBjb21wdXRlciBz
eXN0ZW0uDQo=

--_000_E2C046087E10D943811A0BD0A4E8316D1B57CD1356ankaraanado lu_--

Re: How do YOU set default function/method params?

am 06.10.2009 09:18:15 von List Manager

paragasu wrote:
> why bother, i use available good library
>
> http://swiftmailer.org/
>

Ok, bad example. I already use SwiftMailer. My "problem" though has nothing to do with sending an
email. I should have known someone would take it too literally.

Think a little more general. This is not a specific thing to simply sending an email.

Could be database method calls from a class object, HTML CGI function, etc...

As for a different example. Say you had a function that you had to pass (multiple) arguments to.
But, with those arguments, you had defaults that you would like to inside the function even if they
were not sent when you called your function. But you do not want to be forced into entering ALL the
arguments of a function call in a certain order.

Try this

function createHTMLBox($title, $content, $params=array() ) {

$defaults = array(
'id' = uniq(),
'class' = 'box',
'encode' = TRUE,
);

$params += $defaults;

if ( $params['encode'] ) {
$title = htmlspecialchars($title);
$content = htmlspecialchars($content);
}

# Obviously, I will be using the DomDocument class for this in the real world
# But for simplicities sake, I used the following.
$box = <<


{$title}


{$content}



BOX;

return $box;

}


Then, I call it like this:

echo createHTMLBox('This is my TITLE',
'',
array('encode' => FALSE));

echo createHTMLBox('This is my TITLE',
'');

Both of the above will have different output to the screen.

Hope this clears things up.

--
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

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

Re: How do YOU set default function/method params?

am 06.10.2009 14:17:21 von Al

Jim Lucas wrote:
> Here is a problem that I have had for years now. I have been trying to come up
> with the perfect solution for this problem. But, I have come down to two
> different methods for solving it.
>
> Here is the problem...
>
> >
> function sendEmail(
> $to,
> $from,
> $subject,
> $body,
> $attachments=array(),
> $headers=array()
> ) { # I typically do not put each argument on seperate lines, but I ran
> #out of width in this email...
>
> # do something here...
> mail(...);
>
> }
>
> sendEmail('john@doe.com',
> 'marykate@uhhh.net',
> 'Hi!',
> 'Check out my new pictures!!!',
> $hash_array_of_pictures
> );
>
> Now, we all have a function or method like this floating around somewhere.
>
> My question is, how do YOU go about setting the required entries of the $headers
> array() ?
>
> I see three possible solutions. I want to see a clean and simple solution.
>
> Here are my ideas so far:
>
> function sendEmail(
> $to,
> $from,
> $subject,
> $body,
> $attachments=array(),
> $headers=array()
> ) { # I typically do not put each argument on seperate lines, but I ran
> #out of width in this email...
>
> if ( empty($headers['Date']) ) {
> $headers['Date'] = date('c');
> }
> if ( empty($headers['Message-ID']) ) {
> $headers['Date'] = md5($to.$subject);
> }
> # and the example goes on...
>
> # do something here...
> mail(...);
>
> }
>
> Or, another example. (I will keep it to the guts of the solution now)
>
> $headers['Date'] = empty($headers['Date']) ?
> date('c') : $headers['Date'];
> $headers['Message-ID'] = empty($headers['Message-ID']) ?
> md5($to.$subject) : $headers['Message-ID'];
>
> OR, yet another example...
>
> $defaults = array(
> 'Date' => date('c'),
> 'Message-ID' => md5($to.$subject),
> );
>
> $headers += $defaults;
>
> END of examples...
>
> Now, IMO, the last one is the simplest one and for me, I think it will be the
> new way that I solve this type of problem.
>
> But, my question that I put out to all of you is...
>
> How would you solve this problem?
>
> TIA
>
> Jim Lucas

To me the key word in your question is "default". Here is a send mail example of
how I do it. You'll see that I assign default stuff in the function. The all
caps are constants set in my config file. For extremely high volume
applications, one could memory cache the defaults.

I also use arrays assigned in my config file and then assign the array in the
function using "global". When I do this, I immediately reassign the array so the
function can't change the the assignments made in the config. e.g.,

function foo()
global booArray();

boo2Array= booArray(); Only use boo2Array() in the function.

function pearEmailSend($recipient, $emailSubj, $emailText, $applicEmailAddr)
{
$emailTo = $recipient;
$headers['From'] = $applicEmailAddr;
$headers['To'] = $emailTo;
if(!empty($emailCC)) $headers['Cc'] = $emailCC;
$headers['Return-Path'] = $applicEmailAddr; //or can use SMTP_USER; bounces
are sent to applic address
$headers['Reply-To'] = $applicEmailAddr;
$headers['X-miniReg'] = APPLIC_NAME;
$headers['Date'] = date('r');
$headers['Subject'] = $emailSubj;
$params['debug'] = EMAIL_DEBUG; //Careful, do not leave on, creates a nasty
message for admins
$params['host'] = $_SERVER['SERVER_NAME'];
$params['auth'] = true; //binary, set in config; some servers require auth
$params["username"] = $applicEmailAddr; //was SMTP_USER; //If auth true,
must have value
$params["password"] = SMTP_PW; //If auth true, must have value
$params["localhost"] = $_SERVER['SERVER_NAME'];
$params['persist'] = true; //Default true
$mail_object = @Mail::factory(EMAIL_MODE, $params);

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

Re: How do YOU set default function/method params?

am 06.10.2009 17:50:27 von List Manager

Mert Oztekin wrote:
> IMO, array_merge() is easy to use, however it suppose to use more cpu than other options. If you are a performance freak, i suggest you to not choose array_merge() solution. (also you execute date() and md5() functions even if you wont need to use them)
>
> In the other way, I think first option (with if() ones) seems more readable than other ones.
>

From a performance view, check out these numbers

http://www.cmsws.com/examples/php/functions/speedtests/array _merge.php

To see the source:
http://www.cmsws.com/examples/php/functions/speedtests/array _merge.phps

What a surprising difference in numbers!!!

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

Re: How do YOU set default function/method params?

am 11.10.2009 11:34:14 von Stephan Ebelt

On Mon, Oct 05, 2009 at 05:48:32PM -0700, Jim Lucas wrote:
> Here is a problem that I have had for years now. I have been trying to come up
> with the perfect solution for this problem. But, I have come down to two
> different methods for solving it.
>
> Here is the problem...

[...]

>
> Now, we all have a function or method like this floating around somewhere.
>
> My question is, how do YOU go about setting the required entries of the $headers
> array() ?
>

[...]

>
> END of examples...
>
> Now, IMO, the last one is the simplest one and for me, I think it will be the
> new way that I solve this type of problem.
>
> But, my question that I put out to all of you is...
>
> How would you solve this problem?

I have use this array_merge() approach mentioned in other posts for
quite some time but found that it introduced many bugs when fieldnames changed.
Ie. if the defaults come from a database table and I changed the schema it
caused undefined values during the merging and - worse - sometimes messed up the
inner workings of functions...

Then I heard of the "value object" approach somewhere and found that much more
solid. One would basically define a class where default values are represented
by its properties. Ie:

class vo_email extends vo {
public $to = '';
public $from = '';
public $subject = '(no subject)';
public $body = '';
...
}

the constructor can make sure that absolutly necessary values are required and
set properly - and could complain if something is not right. There could be
methods that add() or set() or change() things. These could also be inherited
from a very generic class "vo" so that this stuff is written once and applies
to all sorts of defaults in the program.
In my app the inherited constructor accepts arrays as parameter and assigns
their elements to the object properties and - by that - overwrites the default
settings. If elements do not match with the defined properties it will trigger
a very visible call trace.

A function like sendEmail() would then require a object of type vo_email as
parameter and would work with its properties internally and can rely on it as
the vo's constructor should have catched anything bad.

If additional logic for the input values is required, it can be added easily:

class dao_email extends vo_email {
...
public function encode_body() {
...
}

public function sanitize_mail_address() {

}
...
}

sendEmail() would then require a dao_email object (dao=data access object) as
input.

stephan

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

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

Re: How do YOU set default function/method params?

am 11.10.2009 22:17:00 von List Manager

Stephan Ebelt wrote:
> On Mon, Oct 05, 2009 at 05:48:32PM -0700, Jim Lucas wrote:
>> Here is a problem that I have had for years now. I have been trying to come up
>> with the perfect solution for this problem. But, I have come down to two
>> different methods for solving it.
>>
>> Here is the problem...
>
> [...]
>
>> Now, we all have a function or method like this floating around somewhere.
>>
>> My question is, how do YOU go about setting the required entries of the $headers
>> array() ?
>>
>
> [...]
>
>> END of examples...
>>
>> Now, IMO, the last one is the simplest one and for me, I think it will be the
>> new way that I solve this type of problem.
>>
>> But, my question that I put out to all of you is...
>>
>> How would you solve this problem?
>
> I have use this array_merge() approach mentioned in other posts for
> quite some time but found that it introduced many bugs when fieldnames changed.
> Ie. if the defaults come from a database table and I changed the schema it
> caused undefined values during the merging and - worse - sometimes messed up the
> inner workings of functions...
>
> Then I heard of the "value object" approach somewhere and found that much more
> solid. One would basically define a class where default values are represented
> by its properties. Ie:
>
> class vo_email extends vo {
> public $to = '';
> public $from = '';
> public $subject = '(no subject)';
> public $body = '';
> ...
> }
>
> the constructor can make sure that absolutly necessary values are required and
> set properly - and could complain if something is not right. There could be
> methods that add() or set() or change() things. These could also be inherited
> from a very generic class "vo" so that this stuff is written once and applies
> to all sorts of defaults in the program.
> In my app the inherited constructor accepts arrays as parameter and assigns
> their elements to the object properties and - by that - overwrites the default
> settings. If elements do not match with the defined properties it will trigger
> a very visible call trace.
>
> A function like sendEmail() would then require a object of type vo_email as
> parameter and would work with its properties internally and can rely on it as
> the vo's constructor should have catched anything bad.
>
> If additional logic for the input values is required, it can be added easily:
>
> class dao_email extends vo_email {
> ...
> public function encode_body() {
> ...
> }
>
> public function sanitize_mail_address() {
>
> }
> ...
> }
>

This is a very interesting approach. How would you initialize the class? Using
a Singleton Method, or a Globally available class variable?


> sendEmail() would then require a dao_email object (dao=data access object) as
> input.
>
> stephan
>
>> TIA
>>
>> Jim Lucas
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>


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

Re: How do YOU set default function/method params?

am 12.10.2009 13:11:20 von Stephan Ebelt

On Sun, Oct 11, 2009 at 01:17:00PM -0700, Jim Lucas wrote:
> Stephan Ebelt wrote:
> > On Mon, Oct 05, 2009 at 05:48:32PM -0700, Jim Lucas wrote:
> >> Here is a problem that I have had for years now. I have been trying t=
o come up
> >> with the perfect solution for this problem. But, I have come down to =
two
> >> different methods for solving it.
> >>
> >> Here is the problem...
> >=20
> > [...]
> >=20
> >> Now, we all have a function or method like this floating around somewh=
ere.
> >>
> >> My question is, how do YOU go about setting the required entries of th=
e $headers
> >> array() ?
> >>
> >=20
> > [...]
> >=20
> >> END of examples...
> >>
> >> Now, IMO, the last one is the simplest one and for me, I think it will=
be the
> >> new way that I solve this type of problem.
> >>
> >> But, my question that I put out to all of you is...
> >>
> >> How would you solve this problem?
> >=20
> > I have use this array_merge() approach mentioned in other posts for
> > quite some time but found that it introduced many bugs when fieldnames =
changed.
> > Ie. if the defaults come from a database table and I changed the schema=
it
> > caused undefined values during the merging and - worse - sometimes mess=
ed up the
> > inner workings of functions...
> >=20
> > Then I heard of the "value object" approach somewhere and found that mu=
ch more
> > solid. One would basically define a class where default values are repr=
esented
> > by its properties. Ie:
> >=20
> > class vo_email extends vo {
> > public $to =3D '';
> > public $from =3D '';
> > public $subject =3D '(no subject)';
> > public $body =3D '';
> > ...
> > }
> >=20
> > the constructor can make sure that absolutly necessary values are requi=
red and
> > set properly - and could complain if something is not right. There coul=
d be
> > methods that add() or set() or change() things. These could also be inh=
erited
> > from a very generic class "vo" so that this stuff is written once and a=
pplies
> > to all sorts of defaults in the program.
> > In my app the inherited constructor accepts arrays as parameter and ass=
igns
> > their elements to the object properties and - by that - overwrites the =
default
> > settings. If elements do not match with the defined properties it will =
trigger
> > a very visible call trace.
> >=20
> > A function like sendEmail() would then require a object of type vo_emai=
l as
> > parameter and would work with its properties internally and can rely on=
it as
> > the vo's constructor should have catched anything bad.
> >=20
> > If additional logic for the input values is required, it can be added e=
asily:
> >=20
> > class dao_email extends vo_email {
> > ...
> > public function encode_body() {
> > ...
> > }
> >=20
> > public function sanitize_mail_address() {
> >=20
> > }
> > ...
> > }
> >=20
>=20
> This is a very interesting approach. How would you initialize the class?=
Using
> a Singleton Method, or a Globally available class variable

as far as I understood/use it: I try to hardcode as many workable defaults =
in
the vo class as possible (ie. see $subject in the example). Then I create o=
bjects
by passing result records from the database (arrays) to the constructor. Th=
at
either returns a object or crashes the application if something is wrong.

Optionally I can create objects without any passed-in parameter which will =
give
one with only the defaults set. Depending on the class' definition those may
have empty properties. These can be set by subsequent code like=20
$object->empty_property=3D'bla'. This way its not much different than using=
plain
arrays except that its still an object which might have additional function=
ality.

in the email example the constructor should probably refuse to return a obj=
ect
unless $to and $from are given. I can't see much use without these two.

stephan

>=20
>=20
> > sendEmail() would then require a dao_email object (dao=3Ddata access ob=
ject) as
> > input.
> >=20
> > stephan
> >=20
> >> TIA
> >>
> >> Jim Lucas
> >>
> >> --=20
> >> PHP General Mailing List (http://www.php.net/)
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >=20

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

Re: How do YOU set default function/method params?

am 12.10.2009 14:44:56 von David Otton

2009/10/12 Stephan Ebelt :

> as far as I understood/use it: I try to hardcode as many workable defaults in
> the vo class as possible (ie. see $subject in the example). Then I create objects
> by passing result records from the database (arrays) to the constructor. That
> either returns a object or crashes the application if something is wrong.

> Optionally I can create objects without any passed-in parameter which will give
> one with only the defaults set. Depending on the class' definition those may

Ok, I'm going to make a case against the use of default values
hard-coded within the class here:

a) Default values mean more code.

The less code you have, the less bugs. Just strip the defaults out,
and they'll never cause errors.

b) Default values hide missing values.

If a value gets mislaid during the build process, the class will still
work, kinda, sortof, but it won't behave as expected. Better to exit
loudly and let the build manager fix the missing value, rather than
try to muddle through on partial data, and fail /really/ impressively
further down the road.

c) You should store all your config options in the same place.

This is simply good practice - it makes life easier for anyone coming
after you who knows that /everything/ is in one place. Zend_Config is
a nice approach - the Config object parses an ini file, and you pass
fragments of the config object to your class constructors. Eg:

$conf = new Zend_Config_Ini( 'config/settings.ini', 'live' );
$db = Zend_Db::factory( $conf->application->databasesettings );

d) Default values lead to assumptions.

MyClass assumes that DbClass connects to localhost if nothing is
passed. This means that MyClass is relying on a feature of DbClass
where it doesn't strictly have to, and DbClass is a little bit less of
a black box.

e) Defaults aren't.

What makes sense on one machine (eg a default of 'localhost' for the
db) may not make sense on another. Rather than tweak the class
defaults to fit the local conditions every time you deploy it, and
have dozens of slightly different versions hanging around, just be
explicit and push the parameters in from outside.

Comments welcome of course, but I've strayed off PHP and into OO design, here.

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

Re: How do YOU set default function/method params?

am 12.10.2009 20:40:55 von Stephan Ebelt

On Mon, Oct 12, 2009 at 01:44:56PM +0100, David Otton wrote:
> 2009/10/12 Stephan Ebelt :
>
> > as far as I understood/use it: I try to hardcode as many workable defaults in
> > the vo class as possible (ie. see $subject in the example). Then I create objects
> > by passing result records from the database (arrays) to the constructor. That
> > either returns a object or crashes the application if something is wrong.
>
> > Optionally I can create objects without any passed-in parameter which will give
> > one with only the defaults set. Depending on the class' definition those may
>
> Ok, I'm going to make a case against the use of default values
> hard-coded within the class here:
>

[...]

I skip a) and b) for now as I mostly agree and first like to clarify...

>
> c) You should store all your config options in the same place.

.... that I do not use this approach for global program config options. If this
was the intent of the original question I may have mistaken it entirely.

(I actually use constants for all on-site configurations and all are defined
in one file, there aren't so many and they can't be modified at runtime (I
think)).

My primary objective for using VOs was to have very strict and clear
definitions for data structures inside the program. Passing loosely defined
arrays from function to function caused too many bugs in my code. Now things
crash much earlier and I get to know problems quicker.

(besides: phpdoc creates nice crosslinks that tell precisely what some
method needs, no long parameter lists but thats also straying off).

stephan


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

Re: How do YOU set default function/method params?

am 17.10.2009 13:00:46 von Carlos Medina

Jim Lucas schrieb:
> Stephan Ebelt wrote:
>> On Mon, Oct 05, 2009 at 05:48:32PM -0700, Jim Lucas wrote:
>>> Here is a problem that I have had for years now. I have been trying to come up
>>> with the perfect solution for this problem. But, I have come down to two
>>> different methods for solving it.
>>>
>>> Here is the problem...
>> [...]
>>
>>> Now, we all have a function or method like this floating around somewhere.
>>>
>>> My question is, how do YOU go about setting the required entries of the $headers
>>> array() ?
>>>
>> [...]
>>
>>> END of examples...
>>>
>>> Now, IMO, the last one is the simplest one and for me, I think it will be the
>>> new way that I solve this type of problem.
>>>
>>> But, my question that I put out to all of you is...
>>>
>>> How would you solve this problem?
>> I have use this array_merge() approach mentioned in other posts for
>> quite some time but found that it introduced many bugs when fieldnames changed.
>> Ie. if the defaults come from a database table and I changed the schema it
>> caused undefined values during the merging and - worse - sometimes messed up the
>> inner workings of functions...
>>
>> Then I heard of the "value object" approach somewhere and found that much more
>> solid. One would basically define a class where default values are represented
>> by its properties. Ie:
>>
>> class vo_email extends vo {
>> public $to = '';
>> public $from = '';
>> public $subject = '(no subject)';
>> public $body = '';
>> ...
>> }
>>
>> the constructor can make sure that absolutly necessary values are required and
>> set properly - and could complain if something is not right. There could be
>> methods that add() or set() or change() things. These could also be inherited
>> from a very generic class "vo" so that this stuff is written once and applies
>> to all sorts of defaults in the program.
>> In my app the inherited constructor accepts arrays as parameter and assigns
>> their elements to the object properties and - by that - overwrites the default
>> settings. If elements do not match with the defined properties it will trigger
>> a very visible call trace.
>>
>> A function like sendEmail() would then require a object of type vo_email as
>> parameter and would work with its properties internally and can rely on it as
>> the vo's constructor should have catched anything bad.
>>
>> If additional logic for the input values is required, it can be added easily:
>>
>> class dao_email extends vo_email {
>> ...
>> public function encode_body() {
>> ...
>> }
>>
>> public function sanitize_mail_address() {
>>
>> }
>> ...
>> }
>>
>
> This is a very interesting approach. How would you initialize the class? Using
> a Singleton Method, or a Globally available class variable?
>
>
>> sendEmail() would then require a dao_email object (dao=data access object) as
>> input.
>>
>> stephan
>>
>>> TIA
>>>
>>> Jim Lucas
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>
Hi,
i would use here two Classes to do this. A Class like Configuration
where the Mail Configuration is implemented. Configuration class load
the Configuration. There you can use Arrays like

$mailConf = array( 'to' => array('type' => 'string', 'required' => true ) );

( You can use the parse_ini_file() method to load the Configuration from
a ini file too )

The Configuration Class can be accessed with get and set methods (your
own or magic)

$conf = $conf->getMailConfiguration(); (return the Array )

And you can use the set method like this:
$conf->setMailConfiguration( Array( values ... ) )

Here you can send a exception if the needed or required items are not set

And set it this into the mail class with

$mail->setConfiguration( mailConfiguration $mailConf )

At this point you can get the values from Object $mailConf

may be helps

Regards

Carlos

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

Re: How do YOU set default function/method params?

am 19.10.2009 19:10:34 von Goltsios Theodore

> Here is a problem that I have had for years now. I have been trying to come up
> with the perfect solution for this problem. But, I have come down to two
> different methods for solving it.
>
> Here is the problem...
>
> >
> function sendEmail(
> $to,
> $from,
> $subject,
> $body,
> $attachments=array(),
> $headers=array()
> ) { # I typically do not put each argument on seperate lines, but I ran
> #out of width in this email...
>
> # do something here...
> mail(...);
>
> }
>
> sendEmail('john@doe.com',
> 'marykate@uhhh.net',
> 'Hi!',
> 'Check out my new pictures!!!',
> $hash_array_of_pictures
> );
>
> Now, we all have a function or method like this floating around somewhere.
>
> My question is, how do YOU go about setting the required entries of the $headers
> array() ?
>
> I see three possible solutions. I want to see a clean and simple solution.
>
> Here are my ideas so far:
>
> function sendEmail(
> $to,
> $from,
> $subject,
> $body,
> $attachments=array(),
> $headers=array()
> ) { # I typically do not put each argument on seperate lines, but I ran
> #out of width in this email...
>
> if ( empty($headers['Date']) ) {
> $headers['Date'] = date('c');
> }
> if ( empty($headers['Message-ID']) ) {
> $headers['Date'] = md5($to.$subject);
> }
> # and the example goes on...
>
> # do something here...
> mail(...);
>
> }
>
> Or, another example. (I will keep it to the guts of the solution now)
>
> $headers['Date'] = empty($headers['Date']) ?
> date('c') : $headers['Date'];
> $headers['Message-ID'] = empty($headers['Message-ID']) ?
> md5($to.$subject) : $headers['Message-ID'];
>
> OR, yet another example...
>
> $defaults = array(
> 'Date' => date('c'),
> 'Message-ID' => md5($to.$subject),
> );
>
> $headers += $defaults;
>
> END of examples...
>
> Now, IMO, the last one is the simplest one and for me, I think it will be the
> new way that I solve this type of problem.
>
> But, my question that I put out to all of you is...
>
> How would you solve this problem?
>
> TIA
>
> Jim Lucas
>
>

You could always check these functions:

http://www.php.net/manual/en/function.func-get-arg.php
http://www.php.net/manual/en/function.func-get-args.php
http://www.php.net/manual/en/function.func-num-args.php

They come with PHP 5.3.

You can use them like Perl's shift if you like.

PS Didn't bother to read the whole thread as you will all understand.

--
Thodoris


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