Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to change a CONSTANT defined in a class file?

Status
Not open for further replies.

sugarferret

IS-IT--Management
Jul 11, 2005
33
US
Hi, I have the folowing php question:
I have 3 files:

the file a.php wich includes this php class: [ include "b.php"; ] (and more code...)
the file b.php wich includes this other php class: [ require_once("c.php"); ] (and other functions of course...)
and the file c.php wich has this definition: [ define('MAX_CUPS', 148); ] (and other definitions...)

My question is how can I overwrite the defined "MAX_CUPS" value for instance if I need sometimes 148, sometimes 400, sometimes 800, etc.
How cai I set this "MAX_CUPS" value again from the a.php file without having to edit the c.php file?
I read that "A constant's value cannot be changed after it is set", but im triying to get help with this issue...

Thanks indvance.

Aldo.


Live as a tortoise.
and rate my mullet:
 
A constant's value cannot be changed after it is set"

This is the problem. You simply can't do it.

The only way you can do what you want to do is to make a new variable, say $var_max_cups in c.php and replace all links to the MAX_CUPS constant in your other files with this new variable.

If you do this, and your other files contain functions in them, remember to declare the new variable at the top of the function where it is used. Like this:

Code:
global $var_max_cups
.

Azza
 
do you know before the script is run what the value should be or can you programmatically derive it?

if so BEFORE the definition is called, assign a variable to hold the derived number and set the definition = to the variable. it will still, once defined, be unchangeable, of course but this route does mean that you don't have to rewrite your code to use variables rather than constants
 
this is a typical sample of code in a.php, so as you can see there are nothing to edit or there are no variables to redefine, because i dont know where in the b.php its calling the 'MAX_CUPS' defined in the c.php
and i cannot edit the b.php and c.php because they are zend optimized.
a question, zend is only a ctypter or it optimizes the runtime too?

<?php
include "b.php";
$cups = new cupsCreator();
$cups->cupsSpec('MyCup1',0,0,120);
$cups->cupsParse();
?>

Aldo

Live as a tortoise.
and rate my mullet:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top