To or not to use Define Constants?


I wish there was a solid rule to follow

I am redoing alot of my code over again. Trying to go OOP… Not actual work, just a pet php project i like to put things I learn into.

I agonize over this. :o :o
[php]define(“PAGE”, “index”);[/php] (this comes from the URL after it is parsed)

I had the above constant referenced all over my code… Procedural

Now am going OOP with classes
So I THINK I wanna use dependency Injection to pass it on to the classes that need it.

It is a public property, (i did want to make it private, but that broke alot of stuff, even using a getter)

But the DEFINE sill works and is easy. and looks soooo very nice. lol

I know that Define is Global space. I can not use a const because it is set at runtime

I keep reading about Encapsulation and that tells me it is bad to use Define(at least how I read it!!! ;D)

Somethings that did not work
[php]if (isset($obj->getPage())[/php]
so now I am either passing the whole ($obj) to another class or the property ($obj->page)

The way I understand Constants
const - Set at compile time. never…never changes
define - Can vary, set up runtime, like url address

but does it matter? just a matter of prefference?

Anyways, I wish there was a solid rule to follow



Everything in programming has a single, common, answer: it depends.

You can use a constant. If you were to do a page class, it would be fine. But, it depends on what you are doing for most things. Have you looked at any frameworks to see how they deal with the issue?


No not yet, been thinking of Laravel since it is more modern than most. thus I assume it uses more oop php.
But I been avoiding it. lol, I am still trying to understand what a Framework is.

Will look for a guide/ tutorial on it. And a small project I can create to put it to use.

If there are other people on these forum that read these posts… would like to hear ya thoughts too…


I would recommend starting with Symfony 3,

Laravel is built on top of Symfony, it is just a more opinionated version.