Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
How to deal with constantes sharing between PHP and JS

I need all your wisdom today.

So I work on a big legacy project at my company and I have to come up with a solution to share constantes between our back-end in PHP 5 (MVC architecture, no framework) and our front-end with JS (angularJS).

In our backend, we have classes that manage our entities, in which we define constantes we work with. These are constantes like char that we use to refer to different possible values of a field.

class Task {
   const STATUS_TODO = 'T';
   const STATUS_DONE = 'D';
}

So when we need to make a condition on these constants, we do like so :

if ( $variable === Entity::STATUS_TODO ) { //do something }

But we don't have such management in our front-end, so you often find conditions like :

if ( variable === 'T' ) { //do something }

Which is a terrible thing to do, we all know it.

The point would be to share constantes between the back and the front, so that if we need to change something or add a new constantes, we just have to do it in one place.

I know this is a pretty common problem in web development, and I was wondering how you guys would advise to solve it.

Thanks for your time, have a great day

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If possible, collect your constants within special (abstract) classes within a central directory like app/Constants in PHP and src/constants in JS. It is then possible (though I never did that on my own) to create an update-script that will parse the constants from one project and sync them to the other. Both languages can be generated pretty easy so you can even approach a bi-directional solution.

Finally you could set up CI/GitHooks to execute the sync on every commit/push/merge.

In the past I ended up with copying the constants from the PHP project to the JS-Client and then re-format them for JS via RegEx.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!