 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Ryan Gantt's Blog: Anonymous recursion in PHP
by Chris Cornutt August 11, 2011 @ 10:55:35
In a recent post to his blog Ryan Gantt looks at an interesting way to get around a limitation in PHP dealing with anonymous recursion and closures that throws a Fatal error when called.
Turns out that variables called as functions must be an instance of Closure, an instance of a class which implements __invoke(), or a string representing a named function in the global namespace. In the anonymous function body above, $fibonacci is none of these. It is an undeclared, free variable in the closure created by the anonymous function. At the time when it's called, it hasn't been bound-hence the Notice that you would have gotten if error reporting were set at a high enough threshold - and therefore can't be called as anything, let alone as a function.
He tried using the "use" functionality PHP closures have to bring a variable/object/etc into the scope of the running function, but it still threw an error. As it turns out, the combination of "use"-ing the object and calling it by reference handles things correctly. He takes this method and applies it in two examples - one call in an array_map function and another in an array_reduce.
voice your opinion now!
anonymous recursion reference invoke closure
Andrea Giammarchi's Blog: PHP Serialization And Recursion Demystified
by Chris Cornutt September 09, 2009 @ 15:39:11
Andrea Giammarchi has posted a new item to his blog looking at freezing and unfreezing objects and variables in PHP apps. Specifically he points out a few gotchas to watch out for.
PHP has different in-core callbacks able to help us with daily deployment, debug, improvements. At the same time, PHP is loads of intrinsic "gotcha", too often hard to understand, hard to explain, or simply hard to manage. One common problem is about debug, caching, or freezing, and the way we would like to debug, cache, or freeze, variables.
He presents the problem - serializing variable information to "freeze" it and how recursion can cause problems down the line. He looks at the two different kinds of recursion (recursion and recursion by reference) and, after looking at a few possible solutions to fix things, eventually comes down to a way to remove the recursion "without losing it".
voice your opinion now!
recursion serialize tutorial freeze
Derick Rethans' Blog: PHP's segmentation faults GDB-fu
by Chris Cornutt October 08, 2008 @ 09:32:09
Derick Rethans has shared a quick tip for locating a code crashing kind of problem with your application when something like XDebug isn't around.
However, because we as PHP developers are lazy, provide a few GDB tricks to make this easier. First of all, it's only really going to work if you haven't stripped the symbols from your PHP and Apache binaries. Secondly, you still need to have the PHP source lying around somewhere.
He suggests using GDB to run the backtraces and create a file to help you track down the infinite recursion problem that could be giving you issues.
voice your opinion now!
segfault gdb infinite recursion source backtrace
Debuggable Blog: XPath on PHP Arrays (Setextract)
by Chris Cornutt September 26, 2008 @ 10:25:23
On the Debuggable blog there's an interesting post where Felix talks a bit about something I've seen requested quite a bit - a method for locating information in an array. His answer is an XPath-style query system to root out your custom information.
One of the requirements [of the original Set::extract method] was that the new method would need to be faster or at least as fast as the old implementation. My first attempts were big failures. Not only did the solutions I came up with contain tons of bugs. No, they were are also a lot slower the old extract function. A few benchmarks later and I discovered the biggest bottleneck in my implementation: Recursiveness.
He notes that no doing things recursively (not just in this situation, but ever) can help with a speed boost. In his example, a small change made all the differences and the XPath implementation in the CakePHP core makes grabbing information from any array simple.
While the implementation does not support full XPath (and probably won't in future), feel free to make suggestions on additional selectors or the idea in general.
voice your opinion now!
xpath array cakephp framework search recursion
DevShed: Developing a Discussion Forum in PHP with Recursion (Part 3)
by Chris Cornutt May 15, 2006 @ 11:44:01
On DevShed today, they've posted this new tutorial continuing their "recursion in PHP" series - "Developing a Discussion Forum in PHP with Recursion".
After covering in detail how to define recursive method and functions, the question is: what comes next? Luckily, there's vast terrain to explore with reference to using recursion in PHP. As I said in previous articles of this series, recursion can be used in cases where a specific tree structure or a linked list needs to be navigated, in order to display, add, delete or edit its values. It's exactly for that reason that this last article will be focused on building an extensible discussion forum, which precisely uses a tree structure (implemented on a single MySQL database table) for displaying forum messages and adding new posts.
Using what they've taught in the first two parts of the series, they put it to good use, giving you a step-by-step guide to a simple recursive forum. They start with the database structure (always a good thing) and work out from there, creating the "ThreadProcessor" class and fetch functionality to grab the thread's contents. They also include a bit of functionality to create threads as well. It's not much more than that, so don't expect too much, but it is a great place to start.
voice your opinion now!
recursion forum message board part3 fetch view recursion forum message board part3 fetch view
DevShed: Using Recursive Methods in Object-based PHP Applications (Part 2)
by Chris Cornutt May 09, 2006 @ 06:25:54
DevShed has posted part two of their series dealing with recusion in PHP, this time with a focus on its use in a more object-oriented environment.
Welcome to the second tutorial of the series "Recursion in PHP." Comprised of three parts, this series introduces the fundamentals of recursion in PHP, including the definition and use of recursive functions in procedural PHP scripts, as well as the creation of recursive methods in object-oriented Web applications.
Now, in this second part of the series, I'll explore some advanced uses of recursion in PHP, particularly in the terrain of object-oriented programming. I will develop a couple of object-based applications which use recursive methods for accomplishing their tasks. By the end of this tutorial, you should have a pretty solid grounding in how to define recursive methods within your own PHP classes.
They start with a simple example of OOP with recursion, handling some "HTML widgets" to be output to a page (div, h1, p, and ul tags). With this library created and in place, they work up a "generator" class to actually build the page dynamically. Finally, they bring it all together with the creation of a simple template processor for simple page creation.
voice your opinion now!
recursion object-oriented tutorial part2 template html recursion object-oriented tutorial part2 template html
DevShed: Fundamentals of Recursion in PHP (Part 1)
by Chris Cornutt May 02, 2006 @ 07:31:46
DevShed has posted their latest tutorial today, a look at some of the basics of recursion and working with it in PHP.
Iteration is a straightforward concept. Recursion is a bit more complicated; it can be defined as a regular function that calls itself. PHP supports recursive functions. This article, the first of three parts, will explain recursive functions and help you see why they are useful.
Considering the important role that recursion plays in most programming languages, and specifically in PHP, over this series I'll be demonstrating how to define and use recursive functions with numerous code samples, thus you can learn quickly how to include them in your own PHP scripts.
If you've never used recursion, you're in luck - they start from the very beginning, explaining it with a simple example of pushing the entire contents of an array (with subarrays) out to a file. They use this exmaple as a base to improve the function, adding the function to write the data out. They finish it off with two handy recursive functions for everyday use - one to escape the entire contents of an array and one that does the same, but checks to see if magic quotes is on.
voice your opinion now!
tutorial fundamentals recursion basic example array tutorial fundamentals recursion basic example array
|
Community Events
Don't see your event here? Let us know!
|