Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda