SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,863
    Mentioned
    5 Post(s)
    Tagged
    2 Thread(s)

    Using Associative Arrays Inside Classes

    I tried searching for the answer, but the Sitepoint server is not responding to the search request. Perhaps maintainence is going on...

    I cannot figure out how to use an associative array inside a class. Here is a brief snippet:

    Code:
    class member
    {
     	public $security;
     	
     	function __construct() 
    	{	
    	 	$this->security['logged_in'] = false;  // Intialize variables 
    	        $this->security['member_id'] = '';
    	}
    // I know the variables could be intialized outside the constructor, but I just am trying to get these damn arrays to work!
    
    // $this->security[$logged_in] just generates an error, as well.  I saw an example of this somewhere...
    The above does not work. I also tried public

    $security = array() and setting variables using => but that did not work.

    I need to fill a $security array with information about the particular member. I am splitting the code into as many small, discrete parts as possible for maximum code reuse among various websites. So there will be numerous functions operating on this array.

    Any ideas about what I'm doing wrong?

    Thanks!

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is working fine for me.
    Code php:
    class member{
    	public $security = array();
    	function __construct(){	
    		$this->security['logged_in'] = true;  // Intialize variables 
    		$this->security['member_id'] = '';
    	}
    }
    $mem = new member;
    if($mem->security['logged_in'] == false){
    	echo "false";
    }else{
    	echo "true";
    }

    I am not sure what exactly you want but the above code is fine for me and did not any error.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,863
    Mentioned
    5 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by rajug View Post
    This is working fine for me.
    Code php:
    class member{
    	public $security = array();
    	function __construct(){	
    		$this->security['logged_in'] = true;  // Intialize variables 
    		$this->security['member_id'] = '';
    	}
    }
    $mem = new member;
    if($mem->security['logged_in'] == false){
    	echo "false";
    }else{
    	echo "true";
    }

    I am not sure what exactly you want but the above code is fine for me and did not any error.
    That's pretty much what I want.

    I'll try it out later. My example wasn't working earlier.

    Thank you very much for the reply. I appreciate your time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •