Should i use constants or globals for paths

hey everyone i;m back lol… got a problem should i use constants or globals to define path locations i was doing it like this…

// Admin Dir

const ADMINDIR = ‘/Admin’;|

or should i do it like this

// Admin Dir

public $adminDir = ‘/Admin’;|


Why do you think you need to do either?


thanks for replying to answer your question i am not exactly sure how i should do it… i seen both of those in different scripts but i was using the first method constants i am using the second method now but i read somewhere its bad to use globals is that true?. but actually i guess the right question should be how should it be done…


Now your on the right track. So lets start with this… What is the actual problem you are trying to solve?

Well, I can’t answer your question directly, but using global in general has always been consider bad programming.

well i am trying to figure out how should i do the path directories were the different dirs are so it knows were to look for like class files or image files ect. like i said i’ve seen it done both ways not sure how to do it and what’s the best way to do it… i seen on a site called thecodingmachine best practices and it says globals are bad so trying to stay away from those. that is an excellent site it tells you the correct way to do classes and everything just about… it is a very good resource for newbies like me…


Thanks Strider64 for answering my question


Unfortunately, the terminology you are using is incorrect, so any information based on just the ‘global(s)’ keyword isn’t relevant.

The first example is a class constant. The second example is a class property (variable.) Neither of these are refereed to as globals in the common php usage of breaking function/class scope by making a main php variable available inside a function/class using the global keyword or $GLOBALS variable (non-class, php defined constants are automatically available throughout all scopes within a script.)

Both of these have class visibility - public, private, and protected. Ref: PHP: Visibility - Manual Without a specific visibility keyword, e.g. your class constant example, that class constant has public visibility, as does the class property example with the explicit public keyword. This means that both of these values are accessible outside of the class. If it is this access you are trying to prevent, which would be the answer to the question of what is the actual problem you are trying to solve, you would use either private or protected visibility.

Thanks phdr i went to that site and i think i know how to do it now i should this…

private $adminDir = ‘/Admin’;


protected $adminDir = ‘/Admin’;


Thanks Snoop

No. Hard coding a path in a class is a code smell and points to a poorly designed architecture. You will do much better to answer my question so we can give you the correct advice.

K the problem i have is i dont know exactly how it should be done i thought those 2 ways i explained is how i thought it should be done but its not… the page u posted ealier says…

public - available anywhere

private - is only available in the class or inherited classes

protected - available only inside the class


so the actual problem is i don’t know what they should be set to public, private or protected… how do i figure that out?

u are right im trying to prevent access to the files if they dont have access to them.

so things like database, cache, sessions should be set to private or protected correct? am i close? i’m confused lol sorry…

Thanks Snoop

How WHAT should be done. You keep asking about your ATTEMPTED SOLUTION to the REAL problem. This is a classic XY Problem.

THIS is the REAL problem I was asking for.
“im trying to prevent access to the files if they dont have access to them”

In this case, if you want only authorized users to access the admin sub directory, you first need to authenticate the user and get their permissions if there are multiple access levels, then set a session that will pass a check in the admin to allow them in there.

Your going to have to show more than a single hard coded path piece of code if you want any real answers. We have no idea what you have going on. How about posting the actual class.

What is the site architecture? Do you have a single point of entry like you “should” or are you serving individual pages with each url.

If you can, it would be helpful if you put your app on GitHub so we can review it as a whole.

yes exactly that’s what i an trying to do there is different levels admin, normal user ect. they log on then depending on there user level determines what they can access or not access…

i’m working on it lol sorry had a bad migraine last night couldn’t do anything…

yeah there is a single entry point…

yeah i really need to clean up the code before i do that it is a little messy i got stuff i need to get rid of cause i don’t need it and just cleanup and organize the code better i will as soon as i do that…

Thanks Snoop

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service