Skip Navigation LinksHome > Categories > Code from a Category

PHP-circuit and a bit of reflection

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1958
Add Date: 01/30/2012
It will be very short notice and more experimental. Last night saw Sinatra (framework based on Ruby) and I he conquered his ease. But in general I really like Ruby, it always will learn in the near future, but now let's talk about PHP. Note boils down to what I have tried to implement a functional Sinatra in PHP. Such a bike, you say, big set, and no doubt you're right. But I found in today's bike is very much useful for themselves. Yes, and "framework" (framework until it is very difficult to call, but a start) to be very small - about 40 lines.


The framework can handle GET requests, considering routing, which hurt the regular expression with any number of parameters. With the release 5.3 version of PHP we have become available so-called closure or an anonymous function (as you like). That is precisely due to the closures of a framework to be very small. When he came up with the name (after all, every programmer should call their offspring), the internet came across a picture giraffe, and acquired the name of my framework (Jirafa Spanish style).


The practical purpose of a framework can not boast, but a possible future, he will acquire functional, and then there will be a real need to use it. And now it's a visual aid on the use of closures. To create the application you want to create an application class and assign specific routes function that will handle the request. Method setRegistry () is used by me in order to convey all the necessary parameters of the function, it can be and template engines, and PDO objects, but generally anything you can pass. And then a few lines of code to demonstrate. I believe that all should be very clear, so let's return to the class of the framework (for the moment "framework consists of a single file, two if you count. Htaccess)


Get () method in it is very simple, it writes the processed route and parameters with which this method was called (we will need in future to fill in the parameters of the function). For the convenience of returning to this same object in order to reduce the application code. Render () method finds a match saved by get () routes and $ _SERVER ['REQUEST_URI']. If a match is found, then we're using reflection (Reflection), we obtain the data from our circuit (both are variables, and their number). Next, using a remarkable method call_user_func_array (), you can call the function or method with a variable number of arguments, we call our exception, passing as parameters, in addition requested, and another variable $ this-> _registry, which, as I mentioned above may contain supplementary information necessary for the application.


In this note we briefly met with the closures and reflection. Invented another little bicycle with square wheels, which I hope, lifted you up and you added a bit of practice and thinking about the future use of mineral structure wonderful language PHP.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.