On Wed, 18 Jun 2008 19:18:59 +0200, Jeff <jeff@spam_me_not.comwrote:
Quick questions I can't find answers to:
Is there a shortcut to do this:
if(! $some_var){$some_var = 'some_other_value'}
I'm used to this: $some_var ||= 'some_other_var'; PHP manual doesn't
show an assignment operator for this that I can find. Is this just
missing or should I be thinking differently?
There just isn't one. No crazy perl like syntax here :P
Alternatives are a ternary operator ( $somevar = $somevar ? $somevar :
'another value'), or the vary hideous: $somevar || $somevar =
'some_other_value';
I'd advise you to keep using the method you mentioned in your post.
Legibility is important.
I usually have some default configuration values in an associative
array and I'd like to have easy access to those in a class. I notice
that global does not work in a class.
It does work in a class.
<?php
//global scope
$var = 'bar';
$arr = array('foz');
class Foo{
function check(){
global $var,$arr;
echo $var;
print_r($arr);
}
}
$a = new Foo();
$a->check();
?>
Most likely, what you think is in global scope is actually not global.
On a side note: Keep in mind what they say about global & references at
http://www.php.net/manual/en/languag...bles.scope.php though...
Can I "define" an array like I would a scalar and what would be the
syntax?
What do you mean 'define an array like a scalar'?
>
And finally, the arrow "->" operator. It looks to me that it can point
at anything: scalar, function, array... Does this mean that I should
make sure I use different names for functions, scalars and arrays?
Both scalars and arrays 'in an object' are just properties (which can be
any type available to you in PHP), so there can't be 2 of them. They're
not handled that differently then you seem to believe, keep in mind PHP
doesn't have strong typing. You can however name a property and a function
the same. For legibility, I'd advise against it.
--
Rik Wasmus
....spamrun finished