Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:24:21+00:00 2026-06-12T10:24:21+00:00

I am thinking about how one would go about creating a PHP equivalent for

  • 0

I am thinking about how one would go about creating a PHP equivalent for a couple of libraries I found for CSS and JS.

One is Less CSS which is a dynamic stylesheet language. The basic idea behind Less CSS is that it allows you to create more dynamic CSS rules containing entities that “regular” CSS does not support such as mixins, functions etc and then the final Less CSS compiles those syntax into regular CSS.

Another interesting JS library which behaves in a (kind of) similar pattern is CoffeeScript where you can write “tidier & simpler” code which then gets compiled into regular Javascript.

How would one go about creating a simple similar interface for PHP? Just as a proof of concept; I am only trying to learn stuff. Lets just take a simple use case of extending classes.

class a
{
    function a_test()
    {
        echo "This is test in a ";
    }
}

class b extends a
{
    function b_test()
    {
        parent::a_test();
        echo "This is test in b";
    }
}

$b = new b();
$b->b_test();

Suppose I want to let the user write class b as (just for the example):

class b[a] //would mean b extends a
{
    function b_test()
    {
        [a_test] //would mean parent::a_test()
        echo "This is test in b";
    }
}

And let them later have that code “resolve” to regular PHP (Usually by running a separate command/process I would believe). My question is how would I go about creating something like this. Can it be done in PHP, would I require to use something like C/C++. How should I approach this problem if I were to go at it? Are there any resources online? Any pointers are deeply appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:24:22+00:00Added an answer on June 12, 2026 at 10:24 am

    Language transcoders are not as easy as one might think.

    The example you gave can be implemented very easily with a preg_replace that looks for class definitions and replaces [a] with extends a.

    But more complex features need a transcoder which is a suite of smaller logical pieces of code.

    In most programmer jargon people incorrectly call transcoders compilers but the difference between compilers and transcoders is that compilers read source code and output raw binary machine code while transcoders read source code and output (a different) source code.

    The PHP (or JavaScript) runtime for example is neither compiler nor transcoder, it’s an interpreter.

    But enough about jargon let’s talk about transcoders:

    To build a transcoder you must first build a tokenizer, it breaks apart the source code into tokens, meaning that if it sees an entire word such as ‘class’ or the name of a class or ‘function’ or the name of a function, it captures that word and considers it a token. When it encounters another token such as an opening round bracket or an opening brace or a square bracket etc. it considers that another token.

    Luckily all of the recognized tokens available in PHP are already easily scanned by token_get_all which is a function PHP is bundled with. You may have some trouble because PHP assumes some things about how you use symbols but all in all you can make use of this function.

    The tokenizer creates a flat list of all the tokens it finds and gives it to the parser.

    The parser is the second phase of your transcoder, it reads the list of tokens and decides stuff like “if token[0] is a class and token[1] is a name_value then we have a class” etc.. after running through the entire list of tokens we should have an abstract syntax tree.

    The abstract syntax tree is a structure that symbolically retains only the relevant information about a the source code.

    $ast = array(
        'my_derived_class' => array(
            'implements' => array(
                'my_interface_1',
                'my_interface_2',
                'my_interface_3'),
            'extends' => 'my_base_class',
            'members' => array(
                'my_property_name' => 'my_default_value',
                'my_method_name' => array( /* ... */ )
            )
        )
    );
    

    After you get an abstract syntax tree you need to walk through it and output the destination source code.

    The real tricky part is the parser which (depending on the complexity of the language you are parsing) may need a backtracking algorithm or some other form of pattern matching to differentiate similar cases against one another.

    I recommend reading about this in Terence Parr’ book http://pragprog.com/book/tpdsl/language-implementation-patterns which describes in detail the design patterns needed to write a transcoder.

    In Terrence’ book you’ll find out why some languages such as HTML or CSS are much simpler (structurally) than PHP or JavaScript and how that relates the complexity of the language parser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was thinking about creating an iPhone app front end for one of our
I'm creating a PHP website which involves users signing up, and I'm wondering about
Not sure where to start with this one ... I'm thinking about creating a
I was thinking about creating script that would do the following: Get all javascripts
I have been thinking about a neat way of load balancing and one thing
Just thinking about the best way to build an Order form that would (from
I'm thinking about creating a tool to visualize scientific data on a website. For
Recently I've been thinking about securing some of my code. I'm curious how one
I'm thinking about creating an application for the iPhone and Android that will need
I am thinking about creating a database system for images where they are stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.