By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,816 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,816 IT Pros & Developers. It's quick & easy.

undefining functions

P: n/a
I have a master-script that runs for every site I make, and it has a standard
set of functions, but sometimes I would want to override that function for a
particular site, but if I redefine it for that site, PHP will complain about
overriding functions. Since that's a good warning, I wouldn't want to remove
it, but I would want to "undefine" a function before defining it again.

like: function_undefine("drawbox");

Or something like that.

Anyone got any suggestions?

--
Sandman[.net]
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

you don't want to define, undefine and redefine functions or
do you have cpu cycles to waste?

split your functions.inc into modular entities that you can
load on request. load the site-specific things at the end.

request_once('func1.inc');
request_once('func2.inc');
request_once('func3.inc');
request_once('spec1.inc');
request_once('spec2.inc');


Sandman wrote:
I have a master-script that runs for every site I make, and it has a standard
set of functions, but sometimes I would want to override that function for a
particular site, but if I redefine it for that site, PHP will complain about
overriding functions. Since that's a good warning, I wouldn't want to remove
it, but I would want to "undefine" a function before defining it again.

like: function_undefine("drawbox");

Or something like that.

Anyone got any suggestions?


Jul 17 '05 #2

P: n/a
Sandman wrote:
Anyone got any suggestions?


<?php
class Master {
function func1() { echo 'func1'; return true; }
function func2() { echo 'func1'; return true; }
}
class Site_X {
function func1() { echo 'X1'; return true; }
function func2() { echo 'X1'; return true; }
}

if (Master::func1()) Site_X::func2();
?>
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #3

P: n/a
"Sandman" <mr@sandman.net> wrote in message
news:mr**********************@news.fu-berlin.de...
I have a master-script that runs for every site I make, and it has a standard set of functions, but sometimes I would want to override that function for a particular site, but if I redefine it for that site, PHP will complain about overriding functions. Since that's a good warning, I wouldn't want to remove it, but I would want to "undefine" a function before defining it again.

like: function_undefine("drawbox");

Or something like that.

Anyone got any suggestions?


If your _sure_ this is what you want (bearing in mind the overhead involved
in parsing the
master-script functions even though they are not used), look into variable
functions.

I believe this is possible (correct me if i'm wrong)...

in master-script:

function foo_default()
{
echo 'blah blah blah';
}
$foo = 'foo_default';
in custom sctipt:

function foo_redefined()
{
echo 'yada yada yada')
}
$foo = 'foo_redefined'; // overwrite variable with new function name.
to call the function:

$foo();
Jul 17 '05 #4

P: n/a
In article <c3**********@taliesin2.netcom.net.uk>,
"Mark Henning" <ma*******@btopenworld.com> wrote:
If your _sure_ this is what you want (bearing in mind the overhead involved
in parsing the master-script functions even though they are not used), look
into variable functions.

I believe this is possible (correct me if i'm wrong)...

in master-script:

function foo_default() {
echo 'blah blah blah';
}
$foo = 'foo_default';
in custom sctipt:

function foo_redefined() {
echo 'yada yada yada')
}
$foo = 'foo_redefined'; // overwrite variable with new function name.
to call the function:

$foo();


Aaah, ok - while that might work, that would mean I would have to edit all the
scripts that use function foo_default(). It's possible, but I'll see if any
other suggestions come around - thanks!

--
Sandman[.net]
Jul 17 '05 #5

P: n/a
In article <c3*************@ID-203069.news.uni-berlin.de>,
Pedro Graca <he****@hotpop.com> wrote:
Sandman wrote:
Anyone got any suggestions?


<?php
class Master {
function func1() { echo 'func1'; return true; }
function func2() { echo 'func1'; return true; }
}
class Site_X {
function func1() { echo 'X1'; return true; }
function func2() { echo 'X1'; return true; }
}

if (Master::func1()) Site_X::func2();
?>


I could use some commentary here.

After having executed the class Site_X for my site and Master in my initscript
- would func1() echo "X1"?

What does the if do?

--
Sandman[.net]
Jul 17 '05 #6

P: n/a
Sandman wrote:
In article <c3*************@ID-203069.news.uni-berlin.de>,
Pedro Graca <he****@hotpop.com> wrote:
Sandman wrote:
> Anyone got any suggestions?
<?php
class Master {
function func1() { echo 'func1'; return true; }
function func2() { echo 'func1'; return true; }
}
class Site_X {
function func1() { echo 'X1'; return true; }
function func2() { echo 'X1'; return true; }
}

if (Master::func1()) Site_X::func2();
?>


I could use some commentary here.

After having executed the class Site_X for my site and Master in my initscript
- would func1() echo "X1"?


No. With the script above, the function "func1" does not exist.
You can also define a func1 outside of any class and call it with
func1().
You cannot have two functions with the same name. The most similar thing
available is putting them inside /different/ classes ... of course after
that their names are "Class::name" instead of simply "name".
/* rewrite of above script */
<?php
function func1() { echo 'global func1'; }

class Site_X {
function func1() { echo 'Site_X func1'; }
}

class Site_Y {
function func1() { echo 'Site_Y func1 + ', func1(); }
}

func1(); // prints "global func1"
Site_X::func1(); // prints "Site_X func1"
Site_Y::func1(); // prints "Site_Y func1 + global func1"
?>


What does the if do?


That was just to show you a way to call the different functions.
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #7

P: n/a


doesn't make sense to me either.
usually classes are handled in seperate files for flexibility.
what you do here just pushes the problem one level down the line.

if you have to check conditions on classe, you can as well
do conditional includes...


Pedro Graca wrote:
Sandman wrote:
In article <c3*************@ID-203069.news.uni-berlin.de>,
Pedro Graca <he****@hotpop.com> wrote:

Sandman wrote:

Anyone got any suggestions?

<?php
class Master {
function func1() { echo 'func1'; return true; }
function func2() { echo 'func1'; return true; }
}
class Site_X {
function func1() { echo 'X1'; return true; }
function func2() { echo 'X1'; return true; }
}

if (Master::func1()) Site_X::func2();
?>


I could use some commentary here.

After having executed the class Site_X for my site and Master in my initscript
- would func1() echo "X1"?

No. With the script above, the function "func1" does not exist.
You can also define a func1 outside of any class and call it with
func1().
You cannot have two functions with the same name. The most similar thing
available is putting them inside /different/ classes ... of course after
that their names are "Class::name" instead of simply "name".
/* rewrite of above script */
<?php
function func1() { echo 'global func1'; }

class Site_X {
function func1() { echo 'Site_X func1'; }
}

class Site_Y {
function func1() { echo 'Site_Y func1 + ', func1(); }
}

func1(); // prints "global func1"
Site_X::func1(); // prints "Site_X func1"
Site_Y::func1(); // prints "Site_Y func1 + global func1"
?>

What does the if do?

That was just to show you a way to call the different functions.


Jul 17 '05 #8

P: n/a
Sandman wrote:
I have a master-script that runs for every site I make, and it has a
standard set of functions, but sometimes I would want to override
that function for a particular site, but if I redefine it for that
site, PHP will complain about overriding functions. Since that's a
good warning, I wouldn't want to remove it, but I would want to
"undefine" a function before defining it again.

like: function_undefine("drawbox");

Or something like that.

Anyone got any suggestions?


One suggestion is to use objects. Specifically, you could turn your
"master-script" into a class, and then instantiate your sites as children of
the class, something like:

<? /* master-class.php */

class BigKahuna {

/* here go functions */

function OneBigFatMasterFunction() {
}

}

?>

<? /* the_site.php */

require_once("master-class.php");

class TheSite extends BigKahuna {

function TheSite() {
/* this is the constructor function, that gets executed every time
you
instantiate the class
*/
}

function OneBigFatMasterFunction() {
/* this overrides the function above */
}

}

?>

<? /* index.php for the site */

require_once('the_site.php');

$site = new TheSite();

?>

That's all, except that instead of calling the functions defined in the two
class the usual way, you call them as $this->OneBigFatMasterFunction().

Berislav


--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.
Jul 17 '05 #9

P: n/a
Allan Rydberg top-posted:
Pedro Graca wrote:
You cannot have two functions with the same name. The most similar thing
available is putting them inside /different/ classes ... of course after
that their names are "Class::name" instead of simply "name".
Just (mis)using classes as namespaces.
/* rewrite of above script */
<?php
function func1() { echo 'global func1'; }

class Site_X {
function func1() { echo 'Site_X func1'; }
}

class Site_Y {
function func1() { echo 'Site_Y func1 + ', func1(); }
}

func1(); // prints "global func1"
Site_X::func1(); // prints "Site_X func1"
Site_Y::func1(); // prints "Site_Y func1 + global func1"
?>
usually classes are handled in seperate files for flexibility.
OK, doesn't matter if they're in different files.
what you do here just pushes the problem one level down the line.

if you have to check conditions on classe, you can as well
do conditional includes...


The OP wanted to 'undefine' a function. As that is impossible, I thought
maybe creating another of the same name (sort of) would be ok.
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #10

P: n/a
In article <c3**********@ls219.htnet.hr>,
"Berislav Lopac" <be************@dimedia.hr> wrote:
That's all, except that instead of calling the functions defined in the two
class the usual way, you call them as $this->OneBigFatMasterFunction().


Ok, I guess this is what the other posted talked about too - I'll try it out -
thanks!

--
Sandman[.net]
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.