473,387 Members | 1,903 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,387 software developers and data experts.

inherited scope in a function (CFPARAM)

Is there a way to call a function so the function shares the scope of the
caller?

For example, I was playing around with making a php version of the cold
fusion tag CFPARAM

What that cold fusion tag/sorta-function does is:
1) Check if a variable exists
2) If it doesn't, creat it with a default value

I could do it inline like this:
eval("if(empty(\$testVar)) \testVar=null;");

but I don't see how I could do it as a function, like say
phParam("test")
.... is there any way for php to see all variables in the caller's scope
without passing them in or declaring them all global?
Jul 17 '05 #1
2 1948
On Sun, 29 Feb 2004 15:44:40 GMT, Indrid Cold <no****@nowhere.com> wrote:
Is there a way to call a function so the function shares the scope of the
caller?
... is there any way for php to see all variables in the caller's scope
without passing them in or declaring them all global?


Not that I'm aware of.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #2
Indrid Cold wrote:
Is there a way to call a function so the function shares the scope of the
caller?

For example, I was playing around with making a php version of the cold
fusion tag CFPARAM

What that cold fusion tag/sorta-function does is:
1) Check if a variable exists
2) If it doesn't, creat it with a default value

I could do it inline like this:
eval("if(empty(\$testVar)) \testVar=null;");

but I don't see how I could do it as a function, like say
phParam("test")
... is there any way for php to see all variables in the caller's scope
without passing them in or declaring them all global?


The good news is that you can quite easily solve the problem of passing
vars as params, with get_defined_vars, compact() and extract() :

<?php
$glo = array(1, 1, 2, 3, 5, 8);

function caller($arg1, $arg2) {
global $glo;
$loc = 33;
$arr = get_defined_vars();

// show what is defined in the current scope :
echo "<hr>\nIn caller :<br>\n"
. nl2br(var_export($arr, true)) . "<hr>\n";

// and pass all context to the callee
callee(compact(array_keys($arr)));
}
function callee($args) {
extract($args);
unset($args); // optional...
// show what is defined in the current scope :
echo "<hr>\nIn caller :<br>\n"
. nl2br(var_export(get_defined_vars(), true)) . "<hr>\n";
}

caller('meaningoflife', 42);
?>

The bad news is that if you need to share so much state, then your
design is probably a bit flawed.

My 2 cents,
Bruno

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim Schueler | last post by:
Here is some sample code that uses inherited class methods: sub parseHTML::docomment { my $comment = shift ; print $comment, "\n" ; } sub parseASP::AUTOLOAD { use vars qw( $AUTOLOAD ) ;
7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
9
by: Troy Hilbert | last post by:
I've created a class derived from NativeWindow. The public methods are visible to consumers of my class and I don't wish them to be. How does one hide those members? Best --- Outgoing...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
10
by: Smokey Grindle | last post by:
i want to inherit the list view class, but in the inherited class, hide the Header style property and the view property (basically its a detailed list with always clickable headers) how do I keep...
5
by: Shak | last post by:
Hi all. I was led to believe that static methods were not inherited by their subclasses (and since that makes sense, rightly so). However, a subclass I've written is using it's (abstract)...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
13
by: PragueExpat | last post by:
I (think) that I've come up with a pattern that I haven't seen in any publications so far and I would like some feedback. Basically, I was looking for a way to inherit private functions and I came...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.