Jump to content

[SOLVED] Need help with a static array please, reproducible code included (5 lines)


EricIsGood

Recommended Posts

<?php
Class users { 
public static $usersToCacheByID = array();
}

users::$usersToCacheByID[] = 'User1';
users::$usersToCacheByID[] = 'User2';
echo '<pre>usersToCacheByID ' . users::$usersToCacheByID . '</pre>';
?>

 

I expect this to print out 'User1' and 'User2' when I print the array.

 

Thanks for your help,

 

-Eric

Link to comment
Share on other sites

You would need to create an instance of the users class first:

 

$u = new users();
u::$usersToCacheByID[] = 'User1';
u::$usersToCacheByID[] = 'User2';
echo 'usersToCacheByID ' . users::$usersToCacheByID . '';

Link to comment
Share on other sites

The class doesn't look static to me. The array is.  ;)

 

Sorry if I'm being dense but...

 

I tried typing:

static class User {

 

Which gave me a fatal error...

 

According to this page:

http://bryanstamour.com/?p=20

 

I don't need to do anything special to make the class static, so long as it's members are static.

 

Obviously I'm missing something...

 

Thanks for the help,

 

-Eric

Link to comment
Share on other sites

 

Thanks... following that example, I did:

 

<?php
class Foo
{
   public static $my_static = 'foo';
}
print Foo::$my_static . "\n";

?>

 

Which works perfect....

 

the only difference from my example in my first post seems to be that I'm using an array....

Link to comment
Share on other sites

Here's another example...

 

example 1 doesn't work

example 2 does work....

 

I'm so confused...

 

<?php

//example 1:
Class users { 
public static $usersToCacheByID = array();
}

users::$usersToCacheByID[] = 'User1';
users::$usersToCacheByID[] = 'User2';
echo '<pre>usersToCacheByID ' . users::$usersToCacheByID . '</pre>';



//example 2:
class Foo {
public static $my_static;
}

Foo::$my_static = 'hi';
echo Foo::$my_static . "\n";

?>

Link to comment
Share on other sites

Please learn how to print Arrays out.  And yes it did work, you should have mentioned it printed out 'Array'.

 

Class users { 
   public static $usersToCacheByID = array();   
}
   
users::$usersToCacheByID[] = 'User1';
users::$usersToCacheByID[] = 'User2';
echo 'usersToCacheByID: 
';
foreach(users::$usersToCacheByID as $key => $value) {
echo 'key: ' . $key  . ' value: ' . $value . '
';
}

echo '';

Link to comment
Share on other sites

Please learn how to print Arrays out.  And yes it did work, you should have mentioned it printed out 'Array'.

 

Thanks for the help.

 

I was originally using print_r.  Between all my moving code arround I lost the print_r call without realizing it.

 

All is good now.

 

Best,

 

-Eric

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.