Re: [PHP] Easier way to get the name of a variable?

2006-08-16 Thread Richard Lynch
On Tue, August 15, 2006 7:01 pm, Larry Garfield wrote:
 On Tuesday 15 August 2006 16:50, Richard Lynch wrote:

 If the names are not predictable, the array solution is probably
 best,
 as there is movement in the PHP Internals list that may (or may not)
 make it impossible to dynamically add a property to an object.  I've
 lost track of where that thread ended, so apologies if this is a
 non-issue.

 Gah!  Please tell me you're joking.  That would kill one of PHP's best
 features, the fact that you can have dynamic data structures.

 PHP != Java!!!

I was partly wrong.

This would only apply (if it applies at all) for static vars.

class foo { };
$foo = new foo;
foo-whatever = 5; //ok
foo::something = 5; //E_STRICT error, maybe, depending on Internals
flamewar outcome...

Richard Swiss cheese memory Lynch :-)

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Easier way to get the name of a variable?

2006-08-15 Thread Chris W. Parker
Hello,

After some intense searching of Google I found one example at
http://us2.php.net/language.variables on how to get the name of a
variable. But it looks pretty expensive.

?php
  function vname($var, $scope=false, $prefix='unique', $suffix='value')
  {
   if($scope) $vals = $scope;
   else  $vals = $GLOBALS;
   $old = $var;
   $var = $new = $prefix.rand().$suffix;
   $vname = FALSE;
   foreach($vals as $key = $val) {
 if($val === $new) $vname = $key;
   }
   $var = $old;
   return $vname;
  }
?

Anyone aware of a simple language construct(?) that can do this? I'm on
PHP 4.3.9.



Thanks,
Chris.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Easier way to get the name of a variable?

2006-08-15 Thread Richard Lynch
On Tue, August 15, 2006 1:19 pm, Chris W. Parker wrote:
 After some intense searching of Google I found one example at
 http://us2.php.net/language.variables on how to get the name of a
 variable. But it looks pretty expensive.

 ?php
   function vname($var, $scope=false, $prefix='unique',
 $suffix='value')
   {
if($scope) $vals = $scope;
else  $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key = $val) {
  if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
   }
 ?

 Anyone aware of a simple language construct(?) that can do this? I'm
 on
 PHP 4.3.9.

There is no function that can do this, because any given variable may
have several different names based on the current scope.

$foo = 5;
function bar ($x) { return baz ($x); }
function baz ($z) {
  echo What's your name?br /Who's your daddy?br /\n;
  echo vname($z);
}
$foobar = $foo;
bar($foobar);

Do you expect 'x' or 'foo' or 'foobar' as the output of your vname()
function?

I don't even know what you're going to get from reading that hack
above, much less what to expect.

You can come at this backwards by passing in the NAME of a variable,
and using variable variables within the function to get the value.

But 99.9% of the time one does that, one should have been using an
array in the first place, and not variable variables.

If you want to associate a name with a value throughout your program,
you should DEFINITELY be using some kind of structure designed for
that.

Associative arrays work very well for this.

Objects with properties also work.

If the names are not predictable, the array solution is probably best,
as there is movement in the PHP Internals list that may (or may not)
make it impossible to dynamically add a property to an object.  I've
lost track of where that thread ended, so apologies if this is a
non-issue.

The array is probably the more correct construct for un-predictable
names, unless there are pre-existing instances that are immutably and
inherently already bound to the name/value you wish to store.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Easier way to get the name of a variable?

2006-08-15 Thread Robert Cummings
On Tue, 2006-08-15 at 16:50 -0500, Richard Lynch wrote:

 If the names are not predictable, the array solution is probably best,
 as there is movement in the PHP Internals list that may (or may not)
 make it impossible to dynamically add a property to an object.

Yikes!! Do you remember the subject line of the thread? i think that
would break a lot of sites if they removed the ability to dynamically
add object properties.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Easier way to get the name of a variable?

2006-08-15 Thread David Tulloh
Chris W. Parker wrote:
 Hello,
 
 After some intense searching of Google I found one example at
 http://us2.php.net/language.variables on how to get the name of a
 variable. But it looks pretty expensive.
 
 snip
 
 Anyone aware of a simple language construct(?) that can do this? I'm on
 PHP 4.3.9.
 

get_defined_vars() can get you a list of all the currently defined
variables.  If you have the value of the variable, you can use
array_search() to find the (first) key.

I do have some misgivings about what you are trying to do though.  I
can't see a case where this would be the best way to solve a problem.


David

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Easier way to get the name of a variable?

2006-08-15 Thread Richard Lynch
On Tue, August 15, 2006 5:05 pm, Robert Cummings wrote:
 On Tue, 2006-08-15 at 16:50 -0500, Richard Lynch wrote:

 If the names are not predictable, the array solution is probably
 best,
 as there is movement in the PHP Internals list that may (or may not)
 make it impossible to dynamically add a property to an object.

 Yikes!! Do you remember the subject line of the thread? i think that
 would break a lot of sites if they removed the ability to dynamically
 add object properties.

It all started here:
http://marc.theaimsgroup.com/?l=php-devm=114596465003800w=2

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Easier way to get the name of a variable?

2006-08-15 Thread Larry Garfield
On Tuesday 15 August 2006 16:50, Richard Lynch wrote:

 If the names are not predictable, the array solution is probably best,
 as there is movement in the PHP Internals list that may (or may not)
 make it impossible to dynamically add a property to an object.  I've
 lost track of where that thread ended, so apologies if this is a
 non-issue.

Gah!  Please tell me you're joking.  That would kill one of PHP's best 
features, the fact that you can have dynamic data structures.

PHP != Java!!!

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   ICQ: 6817012

If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it.  -- Thomas 
Jefferson

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Easier way to get the name of a variable?

2006-08-15 Thread Robert Cummings
On Tue, 2006-08-15 at 19:01 -0500, Larry Garfield wrote:
 On Tuesday 15 August 2006 16:50, Richard Lynch wrote:
 
  If the names are not predictable, the array solution is probably best,
  as there is movement in the PHP Internals list that may (or may not)
  make it impossible to dynamically add a property to an object.  I've
  lost track of where that thread ended, so apologies if this is a
  non-issue.
 
 Gah!  Please tell me you're joking.  That would kill one of PHP's best 
 features, the fact that you can have dynamic data structures.
 
 PHP != Java!!!

I read the list earlier (thanks Richard) and the final consensus was to
enforce the restriction only if the class is defined as strict.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php