473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

undefining functions

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
10 8220

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
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
"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
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
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
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


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
47
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.