Is it possible to over use Constants?

Am new to PHP, and OOP but…

Is it possible to over use Constants?

const = “77”;

define(“CONTACT_URL”, “localhost/contact.html?”);

Specially in OOP?..

What about something like current Customer ID, status, current loaded URL? … I changes from Page load to page load but it is constant during a page load

These things are retrieves and validated at very start of a page load. I am just wondering why I don’t see more scrips with Constants

I don’t see an edit button… so here is more info to help clarify why I am asking

The reason I was asking is that I am learning php OOP.

I have classes that need parameters that I can get 2 ways.
— Defined as global constants after the Page url is parsed and validated
— Or pass the object I created to those classes? (is this what people call Dependency Injection?)

Well it is turning out that a lot of my others classes required those same parameters as well.

This primary class has stuff like Base URL, current Page URL, customer ID, Location…

I been passing the object around to other classes, but am debating if it would be better off to creating constants.
Is there a cost to passing this object around?
Maybe this should be a static class instead… they will never be more then 1 of it instantiated,
hahaha. as i am talking i kinda am answering myself!!!

I suggest you try out a framework like Symfony or Laravel and see how they do this stuff, I’m sure it will be very helpful.

--- Defined as global constants after the Page url is parsed and validated
Global state is "always" bad
--- Or pass the object I created to those classes?
Sounds better
(is this what people call Dependency Injection?)
yes
Is there a cost to passing this object around?
Everything has a cost, but it is probably negligible

Why do you need all these constants / urls everywhere? Sounds like you would be better off creating ie some kind of router class that you could pass in and your controllers/views could use it to get the actual url’s - kinda like the frameworks above are doing it
[php]$this->router->generate(‘contact’);[/php]
[php]{{ path(‘contact’}) }}[/php]

Thanks, I will check out those frameworks, I have no dived into frameworks yet… I figure since I am still learning PHP, Some of the logic/syntax in frameworks look Greek to me. hehehehe

I just learnt this week what a “Controller Class” was. I new what it was… once I read about it, just did not know the terminology

I assigned a project to myself to learn PHP, not something as simple as spitting out pages for a website. More like an CRM application. List of relational Records, with options to Remove, Add, Edit, Toggle Status.
This way I get exposed to SQL also, jQuery, file generation, merging data, sorting, Cache, dynamic images, PHP tools, Apache, and so on… Even CSS is new to me… been playing with this for over a year, always rewriting as I want to learn something new…
These past 2 months it is all about learning OOP and MVC , best practices, terminology

Anyways
A “Router Class” is that like a link builder? A class that constructs the links that would be displayed through out the pages?

jQuery
http://youmightnotneedjquery.com/
A "Router Class" is that like a link builder? A class that constructs the links that would be displayed through out the pages?
As with pretty much else out there there are no correct answers and a router class can be whatever you want it to be.

This is also a must read

Sponsor our Newsletter | Privacy Policy | Terms of Service