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

||= and global in a class

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?

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. Can I "define" an array like I
would a scalar and what would be the syntax?

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?

Jeff
Jun 27 '08 #1
3 1504
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
Jun 27 '08 #2
Rik Wasmus wrote:
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'),
I thought of that, but it saves little.

or the vary hideous: $somevar || $somevar =
'some_other_value';
Cute though!
>
I'd advise you to keep using the method you mentioned in your post.
Legibility is important.
OK. It's just such a common thing to do!
>
> 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();
?>
Got it. I was using global inside the class here:

class Foo{
global $D;

As long as it is in side a function we are fine.
>
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'?
define('SOME_ASSOCIATIVE_ARRAY['some_key']','some_value');

but I see I can also use the $GLOBAL...

Well, I've got several ways to go now.
>
>>
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.
I suppose :
foreach($some_name AS $some_name){

Is a little convoluted.

Thanks!
Jeff

Jun 27 '08 #3
On Wed, 18 Jun 2008 20:47:14 +0200, Jeff <jeff@spam_me_not.comwrote:
Rik Wasmus wrote:
>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'),

I thought of that, but it saves little.

or the vary hideous: $somevar || $somevar =
>'some_other_value';

Cute though!
> I'd advise you to keep using the method you mentioned in your post.
Legibility is important.

OK. It's just such a common thing to do!
Not in PHP :P
define('SOME_ASSOCIATIVE_ARRAY['some_key']','some_value');
Euhm, you can, it would't be an array though, just a very weird constant
name only callable with constant():
<?php
define('arr["foo"]','bar');
echo constant('arr["foo"]');
?>

Constants can be only scalars (... and resources, but that's an
undocumented feature one should not rely on).
--
Rik Wasmus
....spamrun finished
Jun 27 '08 #4

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

Similar topics

17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
22
by: fd123456 | last post by:
Hi Tom ! Sorry about the messy quoting, Google is playing tricks on me at the moment. > Global.asax is where you normally have the Global Application > and Session variables and code to...
9
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
2
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb...
11
by: Ron | last post by:
I have a web project compiled with the new "Web Deployment Projects" plugin for VS2005. I'm deploying the web project to one assembly and with updateable option set to ON. When I'm running the...
8
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
10
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
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: 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?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.