📜 ⬆️ ⬇️

Syntax Sugar for PHP

Only lazy, more precisely, well, very lazy will not tell about a couple of affectionate PHP. It is worth the moment to criticize the developers for more than half of the attempts to correct the situation with the stuffing of the standard library, inconsistencies and the lack of something that I liked very much in * substituting my favorite language * only worsen it (the situation), and those that do have a positive impact ; Of course, this is a provocation and leads to the inevitable holivaru, but let's try to avoid it. From a rather lengthy reflection on this topic, one idea was born ...

Tell me, do you want to change something in the language, correct it? Getting acquainted with new languages ​​and approaches, personally for me - this thought grew stronger. I tried quite a few approaches, starting from trying to write my own interpreter, not having enough knowledge - the attempt failed, ending with rewriting the php source code (not the interpreter, but the *. Php files) - the native interpreter parses the modified source first, then translates it into code \ saves data and already interprets them, but, having achieved certain results - and this attempt was buried due to its inconvenience and “kostyleoobraznosti.” And, already almost disappointed in this undertaking - the speaker on DevConf helped me (if I am not mistaken - Alexander NightTiger ), without knowing it. The report was about aspect-oriented programming and one of the speaker's requests, which pushed me on the right path, was: "Raise your hands for those who know about php filters."

Having rummaged a bit in the manual, re-reading the Aspect framework source code (there were several completely different ones on the githaba), googling - I finally arrived at the result I was waiting for - “Eureka!”, I exclaimed. The case remained for the small, to sketch on the knee for a couple of hours the working code and the case was in the hat - everything I dreamed about - properties, enumerations, named parameters, normal type conversion and much more, everything one can imagine, without extensions, without crutches and hemorrhoids - here it is happiness! What I spent a few months thinking about, trying to write something and hundreds of stainless steel nerve strings - is it really a dream, so carefully cherished by me, will soon come true? What was my surprise when my “govnokod” (may the society forgive me for such an obscene term) really earned, and even the way I almost wanted.
For seed:
<?php import Accessors, Enum, Properties from std; namespace Ololo { enum Color { Red, Blue, Green, Yellow } class Some { private: $asdasd = 23; $some = 42; public $some { get; set($value) { return (int)$value + 42; } } public function __construct() { echo Color::Red . \Ololo\Color::Blue; } } } 


Next was:
1) Writing a plan (roadmap) project
2) Complete rewriting of that two-hour horror that I sketched
3) Research community. Opinions, wishes, take off \ nafig_ono_nado
')
With the first one there were the most problems, I stopped at the option of embedding what is fully compatible with the language - by default. All type indications in parameters, properties, etc. should be available immediately - it does not break backward compatibility, for the rest I decided to use an approach similar to python - an import construction that indicates:

import from ;

where “WHAT” is the name of the file \ class according to the PSR-0 specification, and “WHERE” is an indication of a set of packages, either standard or manually added \ written by the user. The current version of standard packages includes:
The list itself:
* draft Type cast
* todo function __init - automatically called method when class is initialized (require a file with it)
* todo Properties
* todo Enumerations
* todo Aspects
* todo Named Parameters
* todo Simplified declaration of the scope of properties \ methods
* todo Standardization of all functions and their removal into separate spaces
* todo Scalars as objects

where draft is already done and more or less ready for use, and todo is only at the stage of development / rewriting / discussion.

The second point is also quite turned out. The sources are pretty nice (I think, do not hit hard: D) to the touch: github.com/SerafimArts/Mirror

And finally, under the third point - it is your opinion, dear habrasoobschestvo, that is interesting.

Instead of an afterword I want to note:
- At the moment, the core is completely ready.
- Implemented type conversion in parameters of methods \ functions github.com/SerafimArts/Mirror/wiki/Type-Casting including a working example: github.com/SerafimArts/Mirror/blob/master/examples/TypeCasting/test.php
- No unit tests
- No documentation, but fully documented code
- No installation via Composer
- But there is a collected phar package

Using:
 <?php require('phar://mirror.phar'); //  "lib/Mirror/bootstrap.php" require_mirror('path\to\file.php'); 

and inside the file connected using the require_mirror (or include_mirror) function, including absolutely all internal inclusions (even using standard functions) it will be possible to use the Mirror syntax - this is what I called this section as a reflection of the language, and the packages - glass or prism (Glass ) with which you can change the language.

In subsequent articles (if they happen anyway) I want to acquaint you with the library's internal structure - how to write my own “prism”, how caching works, detailed plans for the future, and anything else that interests you. The project is quite crude (it was written during the holidays on the Black Sea - you know yourself), so the article is aimed specifically at your feedback and wishes, so that you don’t get stuck with the development in advance. Thank you very much for your attention.

UPD: Corrected the English translation of the word "glass"

Source: https://habr.com/ru/post/186656/


All Articles