From tedd.sperling at gmail.com Wed Jun 1 17:10:46 2011 From: tedd.sperling at gmail.com (tedd) Date: Wed, 1 Jun 2011 17:10:46 -0400 Subject: [nycphp-talk] Can Javascript call a PHP program and read the results? In-Reply-To: <81DED520-CEAB-4B69-9ADA-B882B5417325@gmail.com> References: <86E4537A-C718-4079-A886-4D23EBF97946@gmail.com> <81DED520-CEAB-4B69-9ADA-B882B5417325@gmail.com> Message-ID: At 4:05 PM -0400 5/31/11, David Roth wrote: >Hi Tedd. > >Thanks for your e-mail. > >That's returning a graphic and I was looking to get a return of text >so the Javascript could make use of it. Unless I overlooked >something in your example? > >Best regards, > >David David: No problem returning text either. http://webbytedd.com/a/ajax-site/ You just need to figure out how to use AJAX (actually AHAH) to send commands from the client-side browser to the server-side php script to do something. After which, the php script then returns the required text (HTML) that a client-side Javascript routine via DOM scripting uses to alter the current page DOM presenting a different layout to the browser/user. The HTML is there and so is the Javascript. The only thing you need is the slave.php script that receives the commands via a GET (see the Javascript routines). The slave.php script simply takes the command and then sends back the HTML needed to create the next page. It's very simple. You should be able to do this (I figured it out) -- if you can't, then I am available for hire. Please contact me off-list. Cheers, tedd ---- > >On May 31, 2011, at 10:41 AM, tedd wrote: > >>At 2:42 AM -0400 5/30/11, David Roth wrote: >>>I'm using Tumblr.com for a blog platform for a new website. It has >>>a lot of benefits as a blog, but it also has the restriction of >>>not being able to embed PHP code in the HTML theme template. (Yes, >>>I know, my feelings exactly! :-) ) >>> >>>But Tumblr does support Javascript. What I'm trying to do is get >>>the county code of the user so that different links can be >>>presented to them that matches their country. Serving up different >>>links based on the country code would be handled by Javascript. >>> >>>There is a Javascript Google library jsapi, but it doesn't work >>>properly for county code. For example, I'm in the New York City >>>metro area and it says I'm in Canada, not the US. I don't >>>recalling having this kind of problem with PHP scripts, so I'd >>>like to use a PHP script. But I don't know of a way in JavaScript >>>to be able to call a PHP script and make the result accessible >>>with the Javascript so it can serve up the correct country links. >>> >>>I admit I've not done much Javascript programming compared to PHP, >>>so I'm not sure if I'm overlooking something. Maybe this is a AJAX >>>and jQuery task? I'd appreciate it if someone could offer a >>>workable solution. >>> >>>Thanks in advance! >>> >>>David Roth >> >>David: >> >>Certainly a Javascript program can call a php script -- here's a demo: >> >>http://webbytedd.com/b/timed-php/ >> >>There's a description at the site. >> >>Cheers, >> >>tedd >> >>-- >>------- >>http://sperling.com/ -- ------- http://sperling.com/ From dan.horning at planetnoc.com Wed Jun 1 17:17:02 2011 From: dan.horning at planetnoc.com (Dan Horning) Date: Wed, 01 Jun 2011 17:17:02 -0400 Subject: [nycphp-talk] Can Javascript call a PHP program and read the results? In-Reply-To: References: <86E4537A-C718-4079-A886-4D23EBF97946@gmail.com> <81DED520-CEAB-4B69-9ADA-B882B5417325@gmail.com> Message-ID: <4DE6AC4E.2070707@planetnoc.com> On 6/1/2011 5:10 PM, tedd wrote: > At 4:05 PM -0400 5/31/11, David Roth wrote: >> Hi Tedd. >> >> Thanks for your e-mail. >> >> That's returning a graphic and I was looking to get a return of text >> so the Javascript could make use of it. Unless I overlooked something >> in your example? >> >> Best regards, >> >> David I'm sure this doesn't really NEED to be said - but just in case you're not aware, be careful here that you filter requests before you do anything with a get/post variable ;-) especially if you - access a database, a file or any server resource. so lets say you are feeding the ahah call a number and you want that image# $var.jpg you can do this safely only if you make sure that the var is a number and is within the expected range hope that adds a tiny intro in case somehow someone someplace isn't aware of this. :-) -------------- next part -------------- A non-text attachment was scrubbed... Name: dan_horning.vcf Type: text/x-vcard Size: 277 bytes Desc: not available URL: From rainelemental at gmail.com Sun Jun 5 08:36:08 2011 From: rainelemental at gmail.com (Federico Ulfo) Date: Sun, 5 Jun 2011 14:36:08 +0200 Subject: [nycphp-talk] Hey guys Message-ID: I've agreed to trade places with this maniac dude from NYC called Spacemonkey. I'm the developer behind raintpl and rainframework and I'm actually working to prepare the rain, a kick ass cms... I should be in NYC very soon, if you organize anything please let me know! Rain -- Federico Ulfo ? Certified Developer Google & PHP -------------- next part -------------- An HTML attachment was scrubbed... URL: From rmarscher at beaffinitive.com Sun Jun 5 20:29:15 2011 From: rmarscher at beaffinitive.com (Rob Marscher) Date: Sun, 5 Jun 2011 20:29:15 -0400 Subject: [nycphp-talk] Hey guys In-Reply-To: References: Message-ID: <4FF63478-5B11-4740-812F-7B94CF688343@beaffinitive.com> On Jun 5, 2011, at 8:36 AM, Federico Ulfo wrote: > I've agreed to trade places with this maniac dude from NYC called Spacemonkey. > I'm the developer behind raintpl and rainframework and I'm actually working to prepare the rain, a kick ass cms... I should be in NYC very soon, if you organize anything please let me know! Welcome to NYC! I took a look at your framework. Pretty cool. Sounds like Spacemonkey is enjoying Turin. Hope you have a good time in NYC. See you around. Actually, if you get in this week, it's "internet week" so there are a bunch of interesting things happening around town - http://www.internetweekny.com/ -Rob -------------- next part -------------- An HTML attachment was scrubbed... URL: From rainelemental at gmail.com Mon Jun 6 02:10:39 2011 From: rainelemental at gmail.com (Federico Ulfo) Date: Mon, 6 Jun 2011 08:10:39 +0200 Subject: [nycphp-talk] Hey guys In-Reply-To: <4FF63478-5B11-4740-812F-7B94CF688343@beaffinitive.com> References: <4FF63478-5B11-4740-812F-7B94CF688343@beaffinitive.com> Message-ID: I should be there soon, but definitely I'll be there on July, thanks for the info and stay tuned on Rain, it will be fun. - Federico On Mon, Jun 6, 2011 at 2:29 AM, Rob Marscher wrote: > On Jun 5, 2011, at 8:36 AM, Federico Ulfo < > rainelemental at gmail.com> wrote: > > I've agreed to trade places with this maniac dude from NYC called > Spacemonkey. > I'm the developer behind raintpl and rainframework and I'm actually working > to prepare the rain, a kick ass cms... I should be in NYC very soon, if you > organize anything please let me know! > > > Welcome to NYC! I took a look at your framework. Pretty cool. Sounds like > Spacemonkey is enjoying Turin. Hope you have a good time in NYC. See you > around. > > Actually, if you get in this week, it's "internet week" so there are a > bunch of interesting things happening around town - > http://www.internetweekny.com/ > > -Rob > > > _______________________________________________ > New York PHP Users Group Community Talk Mailing List > http://lists.nyphp.org/mailman/listinfo/talk > > http://www.nyphp.org/Show-Participation > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mkonop at gmail.com Mon Jun 6 09:43:37 2011 From: mkonop at gmail.com (M Konop) Date: Mon, 6 Jun 2011 09:43:37 -0400 Subject: [nycphp-talk] JoomlaDay NYC and JoomlaDEVDay NYC announded Message-ID: 10/22: JoomlaDayNYC This is a typical Joomla day with multiple tracks, for people at all levels, interested in learning Joomla and those who already know Joomla. 10/23: JoomlaDEVDay NYC This day is dedicated to Developers only! 1. Are you interested in the Joomla! Framework? Louis Landry will have a 3 hour interactive Workshop based on the Joomla! Framework only...take what you like about Joomla! and leave the rest! 2. Are you interested in pure component development? Geraint Edwards will run a 2 hour interactive Workshop dedicated just how to build Joomla! components, things to think of, and how to make them work. 3. Are you interested in template development? Matt Thomas will be running a template development workshop. 4. Are you interested in managing your development environment using GIT? Forest Mars will be presenting a 2 hour workshop on GIT. 5. Still want more??? There will be more! This Dev day is sure to be a success and a sell out! For either event, visit: www . JoomlaDayNYC . com specific schedules are currently posted. -- Marian Konop Gotham Websites www.gothamwebsites.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From ronald.bradford at gmail.com Fri Jun 10 14:24:35 2011 From: ronald.bradford at gmail.com (Ronald Bradford) Date: Fri, 10 Jun 2011 14:24:35 -0400 Subject: [nycphp-talk] Output Buffering with CLI Message-ID: I am having an issue where I am not getting any output during a long running CLI PHP script. Before any output I do ob_start(); ob_implicit_flush(true); After each print I do ob_flush(); and at the end I do ob_end_flush(); However running my script I never get output as it happens. e.g. $ php example.php > file.log I can confirm output is happening with $ php example.php | tee -a file.log I can see output being generated, however it is not flushed to the output file Any help appreciated. Regards Ronald -------------- next part -------------- An HTML attachment was scrubbed... URL: From rainelemental at gmail.com Fri Jun 10 14:34:14 2011 From: rainelemental at gmail.com (federico ulfo) Date: Fri, 10 Jun 2011 20:34:14 +0200 Subject: [nycphp-talk] Output Buffering with CLI In-Reply-To: References: Message-ID: <5617549301970988098@unknownmsgid> On php.net somebody advised to flush the buffer with ob_end_flush(); ob_flush(); flush(); ob_start(); You can try On Jun 10, 2011, at 8:24 PM, Ronald Bradford wrote: I am having an issue where I am not getting any output during a long running CLI PHP script. Before any output I do ob_start(); ob_implicit_flush(true); After each print I do ob_flush(); and at the end I do ob_end_flush(); However running my script I never get output as it happens. e.g. $ php example.php > file.log I can confirm output is happening with $ php example.php | tee -a file.log I can see output being generated, however it is not flushed to the output file Any help appreciated. Regards Ronald _______________________________________________ New York PHP Users Group Community Talk Mailing List http://lists.nyphp.org/mailman/listinfo/talk http://www.nyphp.org/Show-Participation -------------- next part -------------- An HTML attachment was scrubbed... URL: From ronald.bradford at gmail.com Fri Jun 10 14:48:30 2011 From: ronald.bradford at gmail.com (Ronald Bradford) Date: Fri, 10 Jun 2011 14:48:30 -0400 Subject: [nycphp-talk] Output Buffering with CLI In-Reply-To: <5617549301970988098@unknownmsgid> References: <5617549301970988098@unknownmsgid> Message-ID: Thanks. Using that order actually fails. PHP Notice: ob_flush(): failed to flush buffer. No buffer to flush. however the following did give me what I wanted. ob_flush(); ob_end_flush(); flush(); ob_start(); On Fri, Jun 10, 2011 at 2:34 PM, federico ulfo wrote: > On php.net somebody advised to flush the buffer with > > ob_end_flush(); > ob_flush(); > flush(); > ob_start(); > > You can try > > On Jun 10, 2011, at 8:24 PM, Ronald Bradford > wrote: > > I am having an issue where I am not getting any output during a long > running CLI PHP script. > > Before any output I do > > ob_start(); > ob_implicit_flush(true); > > After each print I do > > ob_flush(); > > > and at the end I do > > ob_end_flush(); > > > > However running my script I never get output as it happens. > > e.g. > > $ php example.php > file.log > > I can confirm output is happening with > > $ php example.php | tee -a file.log > > I can see output being generated, however it is not flushed to the output > file > > Any help appreciated. > > Regards > > Ronald > > _______________________________________________ > New York PHP Users Group Community Talk Mailing List > http://lists.nyphp.org/mailman/listinfo/talk > > http://www.nyphp.org/Show-Participation > > > _______________________________________________ > New York PHP Users Group Community Talk Mailing List > http://lists.nyphp.org/mailman/listinfo/talk > > http://www.nyphp.org/Show-Participation > -------------- next part -------------- An HTML attachment was scrubbed... URL: From garyamort at gmail.com Thu Jun 23 23:04:43 2011 From: garyamort at gmail.com (Gary Mort) Date: Thu, 23 Jun 2011 23:04:43 -0400 Subject: [nycphp-talk] Exceptions vs User Errors Message-ID: <4E03FECB.6060600@gmail.com> I'm confused on the difference in common PHP usage between when one should use an exception and when one should use an error. Most of my confusion comes from the fact that the way both Exceptions and Errors are handled and generated in PHP has changed over the years, with each one being used at times incorrectly because the other did not have all the functionality needed for particular usages. So, to review what my understanding is as of PHP 5.3: When an error is generated in PHP, there are a number of pre-defined types of errors: http://www.php.net/manual/en/errorfunc.constants.php What are especially useful for programmers is differentiating between: Depreciated, Notice, Warning, & Error. As of PHP 5.3 all of the above error levels have both a PHP error level and a "user" error level. An example of the difference between them could be that when running a program that utilizes a PHP framework, if at some point in the code there is a call to the split() function - the program will execute correctly, however a depreciated error will be generated and if such errors are logged, periodic review of logfiles will give programmers a heads up that they have to recode part of the program. Wheras if that framework defines it's own internal functions, and one function is slated to be removed, good programming practice would be for the framework authors to generate a user_depreciated error notice - which can be used in the same manner to update code using that framework. When user errors are generated in PHP, the program may or may not abort when the error is generated. It may or may not log the error message to a file or some other method. All of this depends primarily on how the PHP instance is configured. This makes user errors great for providing information about potential problems with the code when you are not sure whether or not the error should result in the program aborting or just noting a transitory problem. Exceptions can do most of the things user errors are used for, but one thing they cannot do is continue executing from the point of the exception being thrown. Once thrown, the block of code being executed is aborted and the path of coding goes to the exception handler instead. This could be used to generate an error message of some sort, log a problem, etc. As an example, consider a web page where we have a common header, a common footer, and then the main section of the web page. For a particular url, the data being displayed in the main section is restricted to authorized individuals. Error handling: the page can be completely generated by one function. First the header is sent, then the authorization for the main section is checked - if it fails, an error can be generated and an unauthorized message generated for display, if it succeeds the data can be generated - then the footer is sent. The user of the application can decide that they want to do something different[lock the account, abort handling, call the cops] by creating their own error handler function and calling it before they call the third party code. Exception handling: The code needs to be much more object oriented. For the main body of code, you might have an object where you call a generate header, generate content, and generate footer method. In the generate content method, you would have to have a try block around the authorization check and content generation, and if authorization fails throw an exception. You would also need to catch the exception and in the exception handling generate the error message to display for the user. Here it can be much harder to override the default manner in which the exception is handled and add your own method. So what I'm curious regarding is how are these 2 methods used in practice today, as opposed to theory. Especially when considering the use of frameworks where you don't expect a single team of programmers to be writing all the code -but rather you are building on an existing framework which can generate it's own errors and exceptions. -Gary From m.suterski at gmail.com Fri Jun 24 08:08:09 2011 From: m.suterski at gmail.com (Marcin Suterski) Date: Fri, 24 Jun 2011 08:08:09 -0400 Subject: [nycphp-talk] Exceptions vs User Errors In-Reply-To: <4E03FECB.6060600@gmail.com> References: <4E03FECB.6060600@gmail.com> Message-ID: <040941E9-481E-4E12-BB31-C9B3FAF63635@gmail.com> I'm from the school where exceptions are for moments where something happened that a normal execution flow would not expect, like incorrect json format in the response from API. Or user tried to access restricted content without being authorized. On the other hand, something like authorization failure from login form is what login flow expects to happen and would be handled with error message. If something exceptional happened you probably want to break your flow and handle it somehow. If you don't have to it means it was something you expected might happen. As for depreciated functions and internal errors it probably depends on what path particular framework is following, exceptions or error values. Worst thing is when it implements both in different modules. Personally, I prefer exception in this case to give me faster feedback. Marcin On Jun 23, 2011, at 11:04 PM, Gary Mort wrote: > I'm confused on the difference in common PHP usage between when one > should use an exception and when one should use an error. > > Most of my confusion comes from the fact that the way both Exceptions > and Errors are handled and generated in PHP has changed over the years, > with each one being used at times incorrectly because the other did not > have all the functionality needed for particular usages. > > So, to review what my understanding is as of PHP 5.3: > > When an error is generated in PHP, there are a number of pre-defined > types of errors: > http://www.php.net/manual/en/errorfunc.constants.php > > What are especially useful for programmers is differentiating between: > Depreciated, Notice, Warning, & Error. > > As of PHP 5.3 all of the above error levels have both a PHP error level > and a "user" error level. An example of the difference between them > could be that when running a program that utilizes a PHP framework, if > at some point in the code there is a call to the split() function - the > program will execute correctly, however a depreciated error will be > generated and if such errors are logged, periodic review of logfiles > will give programmers a heads up that they have to recode part of the > program. Wheras if that framework defines it's own internal functions, > and one function is slated to be removed, good programming practice > would be for the framework authors to generate a user_depreciated error > notice - which can be used in the same manner to update code using that > framework. > > When user errors are generated in PHP, the program may or may not abort > when the error is generated. It may or may not log the error message to > a file or some other method. All of this depends primarily on how the > PHP instance is configured. > > This makes user errors great for providing information about potential > problems with the code when you are not sure whether or not the error > should result in the program aborting or just noting a transitory problem. > > Exceptions can do most of the things user errors are used for, but one > thing they cannot do is continue executing from the point of the > exception being thrown. Once thrown, the block of code being executed > is aborted and the path of coding goes to the exception handler > instead. This could be used to generate an error message of some sort, > log a problem, etc. > > As an example, consider a web page where we have a common header, a > common footer, and then the main section of the web page. For a > particular url, the data being displayed in the main section is > restricted to authorized individuals. > > Error handling: the page can be completely generated by one function. > First the header is sent, then the authorization for the main section is > checked - if it fails, an error can be generated and an unauthorized > message generated for display, if it succeeds the data can be generated > - then the footer is sent. The user of the application can decide that > they want to do something different[lock the account, abort handling, > call the cops] by creating their own error handler function and calling > it before they call the third party code. > > Exception handling: The code needs to be much more object oriented. For > the main body of code, you might have an object where you call a > generate header, generate content, and generate footer method. In the > generate content method, you would have to have a try block around the > authorization check and content generation, and if authorization fails > throw an exception. You would also need to catch the exception and in > the exception handling generate the error message to display for the user. > > Here it can be much harder to override the default manner in which the > exception is handled and add your own method. > > So what I'm curious regarding is how are these 2 methods used in > practice today, as opposed to theory. Especially when considering the > use of frameworks where you don't expect a single team of programmers to > be writing all the code -but rather you are building on an existing > framework which can generate it's own errors and exceptions. > > -Gary > > > _______________________________________________ > New York PHP Users Group Community Talk Mailing List > http://lists.nyphp.org/mailman/listinfo/talk > > http://www.nyphp.org/Show-Participation From dcech at phpwerx.net Fri Jun 24 09:33:11 2011 From: dcech at phpwerx.net (Dan Cech) Date: Fri, 24 Jun 2011 09:33:11 -0400 Subject: [nycphp-talk] Exceptions vs User Errors In-Reply-To: <4E03FECB.6060600@gmail.com> References: <4E03FECB.6060600@gmail.com> Message-ID: <4E049217.30503@phpwerx.net> On 6/23/2011 11:04 PM, Gary Mort wrote: > I'm confused on the difference in common PHP usage between when one > should use an exception and when one should use an error. I usually set up a custom error handler to report internal problems via a back-channel (email, http to a tracking server, etc), and use Exceptions or other error reporting methods (PEAR_Error, etc) for returning error information destined for the user. A pseudo-code example: function do_something() { // do something in the database $result = db_query(); // if the query fails if (!$result) { // report the technical details via back channel trigger_error(db_error_details()); // return a descriptive error to the user throw new Exception('Couldn't do something, contact support'); } // use $result to do something return true } Of course there are innumerable ways to skin a cat, but this approach has worked out ok for me, and it means that I get back-channel notifications for all errors (php notices, etc). > Most of my confusion comes from the fact that the way both Exceptions > and Errors are handled and generated in PHP has changed over the years, > with each one being used at times incorrectly because the other did not > have all the functionality needed for particular usages. > > So, to review what my understanding is as of PHP 5.3: > > When an error is generated in PHP, there are a number of pre-defined > types of errors: > http://www.php.net/manual/en/errorfunc.constants.php > > What are especially useful for programmers is differentiating between: > Depreciated, Notice, Warning,& Error. Firstly, http://en.wikipedia.org/wiki/Deprecation > As of PHP 5.3 all of the above error levels have both a PHP error level > and a "user" error level. An example of the difference between them > could be that when running a program that utilizes a PHP framework, if > at some point in the code there is a call to the split() function - the > program will execute correctly, however a depreciated error will be > generated and if such errors are logged, periodic review of logfiles > will give programmers a heads up that they have to recode part of the > program. Wheras if that framework defines it's own internal functions, > and one function is slated to be removed, good programming practice > would be for the framework authors to generate a user_depreciated error > notice - which can be used in the same manner to update code using that > framework. > > When user errors are generated in PHP, the program may or may not abort > when the error is generated. It may or may not log the error message to > a file or some other method. All of this depends primarily on how the > PHP instance is configured. Yes, though by setting a custom error handler you can take control of almost all of the errors (those excluded are listed here http://us3.php.net/set_error_handler) and process them however you see fit. > This makes user errors great for providing information about potential > problems with the code when you are not sure whether or not the error > should result in the program aborting or just noting a transitory problem. Yes, except that it is a pretty blunt instrument. You can either return to the program flow where it left off, or completely exit. > Exceptions can do most of the things user errors are used for, but one > thing they cannot do is continue executing from the point of the > exception being thrown. Once thrown, the block of code being executed > is aborted and the path of coding goes to the exception handler > instead. This could be used to generate an error message of some sort, > log a problem, etc. Not entirely true, but the code needs to be aware and have provisions to catch the Exception, whereupon it can take whatever action is necessary. > As an example, consider a web page where we have a common header, a > common footer, and then the main section of the web page. For a > particular url, the data being displayed in the main section is > restricted to authorized individuals. > > Error handling: the page can be completely generated by one function. > First the header is sent, then the authorization for the main section is > checked - if it fails, an error can be generated and an unauthorized > message generated for display, if it succeeds the data can be generated > - then the footer is sent. The user of the application can decide that > they want to do something different[lock the account, abort handling, > call the cops] by creating their own error handler function and calling > it before they call the third party code. Typically you'd want to do at least the access checks before you output anything to the client. > Exception handling: The code needs to be much more object oriented. For > the main body of code, you might have an object where you call a > generate header, generate content, and generate footer method. In the > generate content method, you would have to have a try block around the > authorization check and content generation, and if authorization fails > throw an exception. You would also need to catch the exception and in > the exception handling generate the error message to display for the user. If you want to duplicate the same behavior as a custom error handler you can implement a custom exception handler (http://php.net/set_exception_handler) and perform the same sort of logic as in a custom error handler, with the advantage that code can trap and handle its own exceptions, only unhandled exceptions will bubble up to your handler. > Here it can be much harder to override the default manner in which the > exception is handled and add your own method. See above. > So what I'm curious regarding is how are these 2 methods used in > practice today, as opposed to theory. Especially when considering the > use of frameworks where you don't expect a single team of programmers to > be writing all the code -but rather you are building on an existing > framework which can generate it's own errors and exceptions. To my way of thinking these 2 systems are complementary, and as outlined above typically use them side-by-side for different purposes: trigger_error & php-generated errors for development/debugging and exceptions or other error return mechanisms for communicating errors back to the calling code and eventually to the client. Dan From beef at psyop.tv Fri Jun 24 09:34:40 2011 From: beef at psyop.tv (Wellington Fan) Date: Fri, 24 Jun 2011 09:34:40 -0400 Subject: [nycphp-talk] Exceptions vs User Errors In-Reply-To: <040941E9-481E-4E12-BB31-C9B3FAF63635@gmail.com> References: <4E03FECB.6060600@gmail.com> <040941E9-481E-4E12-BB31-C9B3FAF63635@gmail.com> Message-ID: <4E049270.9060101@psyop.tv> Slightly OT, but: "deprecate" NOT "depreciate". :-) From rainelemental at gmail.com Fri Jun 24 09:38:46 2011 From: rainelemental at gmail.com (Federico Ulfo) Date: Fri, 24 Jun 2011 15:38:46 +0200 Subject: [nycphp-talk] Exceptions vs User Errors In-Reply-To: <4E049270.9060101@psyop.tv> References: <4E03FECB.6060600@gmail.com> <040941E9-481E-4E12-BB31-C9B3FAF63635@gmail.com> <4E049270.9060101@psyop.tv> Message-ID: lol... true. One big difference is that the fatal error stop the execution, instead with an exception you can catch also the fatal errors and manage it, cool! -- Federico Ulfo ? Certified Developer Google & PHP On Fri, Jun 24, 2011 at 3:34 PM, Wellington Fan wrote: > Slightly OT, but: "deprecate" NOT "depreciate". > > :-) > > ______________________________**_________________ > New York PHP Users Group Community Talk Mailing List > http://lists.nyphp.org/**mailman/listinfo/talk > > http://www.nyphp.org/Show-**Participation > -------------- next part -------------- An HTML attachment was scrubbed... URL: From paul at devonianfarm.com Fri Jun 24 11:07:38 2011 From: paul at devonianfarm.com (Paul A Houle) Date: Fri, 24 Jun 2011 11:07:38 -0400 Subject: [nycphp-talk] Exceptions vs User Errors In-Reply-To: <4E03FECB.6060600@gmail.com> References: <4E03FECB.6060600@gmail.com> Message-ID: <4E04A83A.1070402@devonianfarm.com> On 6/23/2011 11:04 PM, Gary Mort wrote: > Exceptions can do most of the things user errors are used for, but one > thing they cannot do is continue executing from the point of the > exception being thrown. Once thrown, the block of code being executed > is aborted and the path of coding goes to the exception handler > instead. This could be used to generate an error message of some sort, > log a problem, etc. > Right. Throwing an exception means that you DON'T want to continue executing at that point, or otherwise you wouldn't be throwing an exception. It's definitely not taking on the role of displaying error messages. > As an example, consider a web page where we have a common header, a > common footer, and then the main section of the web page. For a > particular url, the data being displayed in the main section is > restricted to authorized individuals. > That's how people wrote PHP 10 years ago. In a modern design, the user would visit a PHP page which implements a "controller"; this controller inspects the rest of the URL, decides which "view" to run (which corresponds to the "main section of the web page"), runs the view, stuffs the output into an output buffer, and then runs the template, into which the text from the main section is inserted. In this case you can put the whole controller in a try-catch block or you can put the view or the template in a try-catch block. If your application is more interactive (processes forms) it makes sense to have an "action" that processes each form; after the action runs it decides which view to display -- if there's an error it can go back to the original view with error messages added... It can also display a different form depending on what you inputted. For instance, if you've got a long application form broken up into 10 pages, you might be able to skip from page 3 to page 7 because you've got enough information to know the user doesn't need to fill those pages out. > Exception handling: The code needs to be much more object oriented. For > the main body of code, you might have an object where you call a > generate header, generate content, and generate footer method. In the > generate content method, you would have to have a try block around the > authorization check and content generation, and if authorization fails > throw an exception. You would also need to catch the exception and in > the exception handling generate the error message to display for the user. > Using a modern architecture it's your choice for how object-oriented you want to be. I wrote a microframework that was 79 lines of PHP that entirely used procedural code for the actions, views, and templates. You could add a new action or view by just adding a new PHP file in the right directory with the right name. You can definitely build something that works along the same lines that uses OO and you'd have Symfony or CakePHP or CodeIgniter. I'd say that, however, the simplest microframework that implements the action/view/template gives you 50% of the value that a large framework does. In that kind of architecture, it would make sense for authentication to have a close relationship to the framework. If you've got to write new code to link the authentication system to each page on your site, you're going to f* it up, believe me. An alternative pattern for authentication is to use the "method that does not return"; usually in web authentication, we redirect a user to a login page if we find they aren't logged in. In that case, you can do something like $auth=new Auth(); $auth->required(); on a page for which authentication is required. The required() function sends a redirect header and then calls exit(). The advantage of this is that it's bulletproof against programming errors on the server side. If for instance, it returned false or threw an exception, the security would be dependent on the outside code doing the right thing. If $auth->required() kills the script, it's completely impossible for a programmer using this object to do the wrong thing. > Here it can be much harder to override the default manner in which the > exception is handled and add your own method. BTW, you might also take a look at http://php.net/manual/en/function.set-exception-handler.php