I like PHP (if you don’t like it - please forget about this topic. You don’t have to do a holivar), and I also like one thing that is right everywhere, but not in PHP:
superFunction(foo, bar)[2];
What does this code do? Right! Returns the third element of the array, which returns superFunction () with arguments foo and bar.
')
In PHP syntax, it would look like this:
superFunction($foo, $bar)[2];
Here only this code issues Parse Error. “And rightly so!” - screams of haters of syntactic sugar are heard already. I suggest that they also go far away from this topic, so as not to holivarit and not to prove that it is not necessary (look at what blog I posted it, after all).
On the PHP site, I learned that the developers do not plan to allow this syntax even in version 6. Well damn. I myself - well done! So, over the night I wrote a small class, which, if used correctly, allows you to work with arrays humanly.
Does it work?
Yes, Preparser works. After several hours of debugging, I was able to teach him not to fail, processing one of my “big” applications using the Zend Framework and a thousand things in a row. From which I conclude that it will not fail by default.
It works quickly. Yes, it parses all connected (include, etc.) files and changes everything there as it wants. But Preparser caches the processed code, of course, neatly caches and connects with the usual include when the script is re-run, which does not take extra CPU time at all. By the way, Preparser checks the date of file modification, so you can not worry about clearing the cache with each modified line.
Already otparsennye files perform the construction of func () [] without loss of performance. There is nothing terrible happening at all :)
It works simply. In order to take and translate well, everything is straightforward for a huge, large application on Preparser, you need to write 2 or there I don’t know, 3 lines. If you program by changing global variables in include, then you have to add a few more lines. But you are adults and have not been doing such bjak for a long time, right?
By the way, global variables from include will cease to change, but the command return $ smth; will work.
And also: for compatibility with Zend_View in all the files inside classes, $ this continues to work.
So, if you write using the Zend Framework, then everything will be fine. If you have some other civil framework without ugliness like global variables - most likely, everything will be fine (I didn’t check, I’ll be happy with your feedback!). Well, if not, I don’t know, it’s still worth a try, it's a blessing. At the same time tell me what I'm wrong :)
Oh yes, Preparcer needs PHP branches 5.3. :)
What's up?
Preparser works with files that are loaded through it, as well as files that are loaded by those files that are loaded through it. You probably did not understand the previous phrase. Okay.
Option 1: play around
The bottom line is this: in order to use the func () [2] construct in a php file, it needs to be connected via Preparser. Ie, create, say, index.php and hooray.php:
<? // index.php
// include_path()
require_once '/library/Preparser.php';
// , . !
Preparser\setCachePath('/../data/preparserCache/');
Preparser\requirePreparsed_once('hooray.php');
Now you can use the most terrible constructions in hooray.php (in / tests/dereferencing.php you can find examples):
<? // hooray.php
assert( array('a' => 1, 'b' => 2)['a'] == 1 );
function ret_anything($lol) {
return $lol;
}
assert( ret_anything( array(1, 2, ret_anything(4), ret_anything(array(1, 2, 3))[1])[ ret_anything(array(1, 2, 3))[ret_anything(array(1, 2, 3))[1]] ]) == 2 );
Option 2: transfer the project to Preparser
Here's how I changed index.php for one cool site:
<?
// set_include_path()
require_once '/Preparser.php';
Preparser\setCachePath(APPLICATION_PATH . '/../data/preparserCache/');
//: require_once 'Zend/Application.php';
Preparser\requirePreparsed_once('Zend/Application.php');
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
Added two lines, and then changed one. Very difficult! Then I opened index.php in the browser and waited (long enough, almost a minute) until the cache was formed. After that, everything worked quickly and without a hitch.
Underwater rocks?
I do not know what kind of pitfalls there are. I tried to work carefully. The global namespace is not cluttered. A freshly inclined file will not receive a bunch of variables as a gift; it will not receive any at all, except, perhaps, $ this. Well, you can also connect your global variables with the global directive.
I think you will find these stones faster than me - I will be happy for your feedback!
Where to get?
And, take this from
GoogleCode . Open Source. License New BSD, I did not go into details, but it seems you can use it as you want and you don’t have to open your code :)
There still is not even started manual. I will be glad if someone helps me to write it ...
How to help?
... yes, I will be glad if someone helps me to write manuals and stuff. I’m really bad at speaking English, so I’m a little ashamed of everything I’ve written in README.txt and HOWTOS.txt. I will be grateful for correcting the mistakes there too, but one is a request: do not write about them in the comments here in the factory, so as not to foul up these very comments with temporary and meaningful messages. I have different mails, for example, preparser@va1en0k.net
I will also be grateful for the testing. Well, and - especially - for the patches.
Ficrekvesty required. I think that much more would be worth teaching a php parser.
Thanks for attention!
I did this, of course, out of boredom and I do not promise any support or justification for the necessity of existence. 8)