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

2009-10-11 Thread 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



[PHP] PHP function unpack

2009-10-11 Thread Gabriel Hahmann
Hi,

I'm new to the list and I've search internet and didn't find an answer
to my problem.

I'm converting a perl script to PHP and I've done almost everything.
The only part of the code that I didn't get working on PHP was:

(PERL CODE)
foreach ($b_length,$b_mac,$b_crlf,$b_crlf,$a_body) {
$d_checksum += unpack(%16n*, $_);
}

The idea is to make a 16-bit checksum of all elements
($b_length,$b_mac,$b_crlf,$b_crlf,$a_body).

I'm tried translating the code to:

$arr_checksum = array($b_length,$b_mac,$b_crlf,$b_crlf,$File_Content);

$d_checksum = 0;
foreach($arr_checksum as $check_elem){
$d_check = unpack(n*, $check_elem);
for ($i=0;$i4;$i++){
$d_checksum += $d_check[$i];
}
}

But the result is different. I know that unpack in perl and php are
different, but I cant find a way to translate the format %16n* of perl
to php and generate a 16-bit checksum.

Any help will be appreciated.

[]'s
Gabriel.

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



[PHP] Re: Insult my code!

2009-10-11 Thread Eric Bauman

On 7/10/2009 5:34 PM, Eric Bauman wrote:

Hi there,

I'm in the process of trying to wrap my head around MVC, and as part of
that, I'm attempting to implement a super-tiny MVC framework.

I've created some mockups of how the framework might be used based
around a very simple 'bank', but I'm trying to get some feedback before
I go and implement it, to make sure I'm actually on the right track.

Any thoughts would be much appreciated!

Model - http://www.pastebin.cz/23595
Controller - http://www.pastebin.cz/23597
View - http://www.pastebin.cz/23598
Template - http://www.pastebin.cz/23599


Hi again,

I've done quite a bit of reading, thinking and coding based on all the 
feedback I received from my original post.


I have since come up with this:

Model - http://www.pastebin.cz/23857
Controller - http://www.pastebin.cz/23858
View - http://www.pastebin.cz/23859
Persistence - http://www.pastebin.cz/23860

As before, please feel free to insult my code. ;-) Any and all feedback 
is of course most appreciated.


Best regards,
Eric

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



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

2009-10-11 Thread Jim Lucas
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



[PHP] php exception handling

2009-10-11 Thread Lars Nielsen
Hi,

I am trying to make an exception class that emails the errors to myself.
I have started by using the example by ask at nilpo dot com on
http://dk2.php.net/manual/en/language.exceptions.php.

It work ok but i want it NOT to show the errors on the php-page but only
show the details in the email and then just write eg An error occurred
or so on the webpage. Can anyone give me a hint about how to achieve
this?

Regards 
Lars Nielsen


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



Re: [PHP] Newbie: Array of objects iteration

2009-10-11 Thread Tommy Pham
- Original Message 
 From: MEM tal...@gmail.com
 To: Tommy Pham tommy...@yahoo.com; php-general@lists.php.net
 Sent: Sat, October 10, 2009 2:49:23 AM
 Subject: RE: [PHP] Newbie: Array of objects iteration
 
  
  MEM,
  
  http://www.php.net/language.oop5.reflection
  
  
  Regards,
  Tommy
  
  
 
 
 And brand new world opens in from of my eyes... O.O. 
 
 I will search more info on this on the net... just for the records, as
 properties names is concern, I couldn't find any better I suppose:
 http://pt.php.net/manual/en/reflectionproperty.getname.php
 
 
 I'm just wondering, we call it like this?
 $property = new ReflectionProperty($myproperty);
 $proptertyName = $property-getName();
 
 
 Can we called statically like this?
 $propertyName = ReflectionProperty::getName($myproperty);  - this would
 be nice. :)
 
 The documentation is a little bit lacking, and I'm a little bit newbie, is
 this correct?
 
 Note:
 The array_keys as a possibility to retrieve ALL keys from a given array. It
 would be nice to retrieve ALL properties names at once as well...
 :)
 
 
 
 Regards,
 Márcio
 
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php

Márcio,

Sorry, I don't remember since it's been over 5 years I used reflection.  
Reflection has many uses, 1 of it is that it allow you reverse engineer any PHP 
app ;)

Regards,
Tommy


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



Re: [PHP] php exception handling

2009-10-11 Thread Tommy Pham
- Original Message 
 From: Lars Nielsen l...@mit-web.dk
 To: php-general@lists.php.net
 Sent: Sun, October 11, 2009 2:18:03 PM
 Subject: [PHP] php exception handling
 
 Hi,
 
 I am trying to make an exception class that emails the errors to myself.
 I have started by using the example by ask at nilpo dot com on
 http://dk2.php.net/manual/en/language.exceptions.php.
 
 It work ok but i want it NOT to show the errors on the php-page but only
 show the details in the email and then just write eg An error occurred
 or so on the webpage. Can anyone give me a hint about how to achieve
 this?
 
 Regards 
 Lars Nielsen
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php

Lars,

Here's a pseudo code:

try {
  if (!$valid) {
throw new Exception(Test failed.);
  } else {
   // do something
  }
} catch (Exception $e) {
  // set/print a user friendly message to send to output

  // set a detailed message using debug_backtrace() or any other information 
relevant for easier troubleshooting
  // log and/or email detailed message
}

Regards,
Tommy


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



Re: [PHP] php exception handling

2009-10-11 Thread Larry Garfield
On Sunday 11 October 2009 6:09:46 pm Tommy Pham wrote:

 Lars,

 Here's a pseudo code:

 try {
   if (!$valid) {
 throw new Exception(Test failed.);
   } else {
// do something
   }
 } catch (Exception $e) {
   // set/print a user friendly message to send to output

   // set a detailed message using debug_backtrace() or any other
 information relevant for easier troubleshooting // log and/or email
 detailed message
 }

 Regards,
 Tommy

Actually the else clause is not necessary.  That's one of the nice things 
about exceptions.  If you throw an exception, processing jumps to the 
appropriate catch and never returns.  

try {
  // Do normal stuff.

  if (!$valid) {
throw new Exception('OMG!');
  }

  // Do more normal stuff.
}
catch (Exception $e) {
  // Print user friendly message.
  // Log detailed information or whatever you're going to do.
}


-- 
Larry Garfield
la...@garfieldtech.com

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



[PHP] Re: php exception handling

2009-10-11 Thread Al



Lars Nielsen wrote:

Hi,

I am trying to make an exception class that emails the errors to myself.
I have started by using the example by ask at nilpo dot com on
http://dk2.php.net/manual/en/language.exceptions.php.

It work ok but i want it NOT to show the errors on the php-page but only
show the details in the email and then just write eg An error occurred
or so on the webpage. Can anyone give me a hint about how to achieve
this?

Regards 
Lars Nielsen




Don't sell exception handling short; it can be very useful for processing 
control.  Here is an example of a sequence of user input validations and checks.


All check functions throw an exception, with the details, if they find an error. 
e.g., Tag spen is invalid. Check spelling. This message is rendered so the 
user can correct his/her mistake.


if(!empty($adminMiscSettingsArray['instrText']))
{
 try{
  checkValidTags($validHTMLtags, 
allProxyTagsArray,adminMiscSettingsArray['instrText']);


  checkTagMatching($emptyProxyTagsArray,$adminMiscSettingsArray['instrText']);
  checkTagNesting($emptyProxyTagsArray, $adminMiscSettingsArray['instrText']);
  checkTextLinks($linksProxyTagsArray, $adminMiscSettingsArray['instrText']);
  }
 catch (Exception $e){
   $instrCheckErrorMsg = $e-getMessage();
}

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



[PHP] Need unrounded precision

2009-10-11 Thread Andre Dubuc
Hi,

I need to extract the first digit after the decimal point from a number such 
as 28.56018, which should be '5'.

I've tried a few methods to accomplish this. If I use 'ini_set' I would need 
to know the number of digits before the decimal (which, unfortunately, I 
would not have access to). 

Then I've tried:

?php

$elapsed = 28.56018;

$digit = round($elapsed, 1); // rounds result is '6'
$digit = number_format($elapsed, 1); // still rounds result to '6'

?

What I need is only the first digit after the decimal -- all the rest could 
be 'chopped' or discarded but without rounding the first digit after the 
decimal point. 

Is there any way of doing this?

I'm stumped.

Tia,
Andre

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



Re: [PHP] Need unrounded precision

2009-10-11 Thread Eddie Drapkin
On Fri, Jan 1, 2010 at 9:20 PM, Andre Dubuc aajdu...@webhart.net wrote:
 Hi,

 I need to extract the first digit after the decimal point from a number such
 as 28.56018, which should be '5'.

 I've tried a few methods to accomplish this. If I use 'ini_set' I would need
 to know the number of digits before the decimal (which, unfortunately, I
 would not have access to).

 Then I've tried:

 ?php

        $elapsed = 28.56018;

        $digit = round($elapsed, 1); // rounds result is '6'
        $digit = number_format($elapsed, 1); // still rounds result to '6'

 ?

 What I need is only the first digit after the decimal -- all the rest could
 be 'chopped' or discarded but without rounding the first digit after the
 decimal point.

 Is there any way of doing this?

 I'm stumped.

 Tia,
 Andre

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



Try sprintf with a format of %0.1f.

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



Re: [PHP] Need unrounded precision [RESOLVED]

2009-10-11 Thread Andre Dubuc
Thanks Eddie,

Actually while waiting for some re[lies, I resolved the problem.

I realized that '$elapsed' will always have only two digits before the decimal 
point. Thus I was able to use 'number_format($elapsed, 2);' and to give me 
the desired result.

Thanks for the quick reply!
Andre



On October 11, 2009 08:50:11 pm Eddie Drapkin wrote:
 On Fri, Jan 1, 2010 at 9:20 PM, Andre Dubuc aajdu...@webhart.net wrote:
  Hi,
 
  I need to extract the first digit after the decimal point from a number
  such as 28.56018, which should be '5'.
 
  I've tried a few methods to accomplish this. If I use 'ini_set' I would
  need to know the number of digits before the decimal (which,
  unfortunately, I would not have access to).
 
  Then I've tried:
 
  ?php
 
         $elapsed = 28.56018;
 
         $digit = round($elapsed, 1); // rounds result is '6'
         $digit = number_format($elapsed, 1); // still rounds result to '6'
 
  ?
 
  What I need is only the first digit after the decimal -- all the rest
  could be 'chopped' or discarded but without rounding the first digit
  after the decimal point.
 
  Is there any way of doing this?
 
  I'm stumped.
 
  Tia,
  Andre
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php

 Try sprintf with a format of %0.1f.



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



Re: [PHP] Need unrounded precision

2009-10-11 Thread Jim Lucas

Andre Dubuc wrote:

Hi,

I need to extract the first digit after the decimal point from a number such 
as 28.56018, which should be '5'.


I've tried a few methods to accomplish this. If I use 'ini_set' I would need 
to know the number of digits before the decimal (which, unfortunately, I 
would not have access to). 


Then I've tried:

?php

$elapsed = 28.56018;

$digit = round($elapsed, 1); // rounds result is '6'
$digit = number_format($elapsed, 1); // still rounds result to '6'

?

What I need is only the first digit after the decimal -- all the rest could 
be 'chopped' or discarded but without rounding the first digit after the 
decimal point. 


Is there any way of doing this?

I'm stumped.

Tia,
Andre



FYI: did you know that your computer thinks it is in the future?

You need to check your date settings...

--
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: [PHP] Need unrounded precision

2009-10-11 Thread Andre Dubuc
Yup! I had noticed that I had forgot to reset to today's date after working on 
the code, just after I sent the messages.

Thanks.
Andre



On October 11, 2009 09:14:56 pm Jim Lucas wrote:
 Andre Dubuc wrote:
  Hi,
 
  I need to extract the first digit after the decimal point from a number
  such as 28.56018, which should be '5'.
 
  I've tried a few methods to accomplish this. If I use 'ini_set' I would
  need to know the number of digits before the decimal (which,
  unfortunately, I would not have access to).
 
  Then I've tried:
 
  ?php
 
  $elapsed = 28.56018;
 
  $digit = round($elapsed, 1); // rounds result is '6'
  $digit = number_format($elapsed, 1); // still rounds result to '6'
 
  ?
 
  What I need is only the first digit after the decimal -- all the rest
  could be 'chopped' or discarded but without rounding the first digit
  after the decimal point.
 
  Is there any way of doing this?
 
  I'm stumped.
 
  Tia,
  Andre

 FYI: did you know that your computer thinks it is in the future?

 You need to check your date settings...

 --
 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] security/deployment issue

2009-10-11 Thread Augusto Flavio
Hi everybody,



i have a doubt about my security and deployment methods. Today i manage
several projects and these projects are versioned with subversion. My
environment is something like this:

1. The developer make some update in the source code of a project. (from
your IDE, generally netbeans)
2. The developer commit the modifications to the subversion server after
test it(sure).
3. The project manager sync the files from the dev server to the prod
server(using rsync).


Well, my questions are 2. All about the rsync:

1. For each project we have a ssh user that is used to sync the files(source
code) to the prod server. The problem that i see here is that for each
project i need to have a ssh account to sync these files. This is not so
cool because i need to have severals actived ssh accounts in my prod server.
I'm thinking about the root account to do this work. Is this a good
practice?


2. Does have some another way, more better than the rsync for this
deployment issue?



Thanks.



Augusto Morais


Re: [PHP] security/deployment issue

2009-10-11 Thread James McLean
On Mon, Oct 12, 2009 at 4:06 PM, Augusto Flavio afla...@gmail.com wrote:
 i have a doubt about my security and deployment methods. Today i manage
 several projects and these projects are versioned with subversion. My
 environment is something like this:

 1. The developer make some update in the source code of a project. (from
 your IDE, generally netbeans)
 2. The developer commit the modifications to the subversion server after
 test it(sure).
 3. The project manager sync the files from the dev server to the prod
 server(using rsync).

Sounds mostly fine. I assume you have other testing going on before
deployment to production, though.

 Well, my questions are 2. All about the rsync:

 1. For each project we have a ssh user that is used to sync the files(source
 code) to the prod server. The problem that i see here is that for each
 project i need to have a ssh account to sync these files. This is not so
 cool because i need to have severals actived ssh accounts in my prod server.
 I'm thinking about the root account to do this work. Is this a good
 practice?

The root account is not a very good idea for this. You could create a
'service' account that is used exclusively for transferring the files
to the server. To allow this user access to the various source
directories you can use something like ACL's or perhaps even regular
UNIX file permissions may work if your needs aren't very complex.

 2. Does have some another way, more better than the rsync for this
 deployment issue?

Rsync should work fine, but personally I like to see exactly which
changes are being deployed especially when deploying to production.
While I realise this recommendation is not Open Source software, I
have found it to be an excellent piece of software for this task. I
use Beyond Compare which has the ability to connect over SFTP or SCP
as well as regular FTP. It allows you to 'diff' the files as you go
and view exact changes and you can transfer only the changes you want
or whole files if you choose to. I would not be surprised if an Open
Source equivalent exists.

Cheers,

James

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