Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda