Re: Retrieving values from array on a class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Saturday 14 October 2006 19:09, AR wrote:
> Hello,
>
> I've rewritten my class as you told me:
>
> ----------------------------------------------------------
>  class returnConfigParams
>
>  {
>
>   private static $instance = NULL;
>
>   var $a;
>   var $b;
>   var $c;
>   var $d;
>
>   private function __construct() {
>   }
>
>   // function that get the database parameters from "properties.php"
>
>     public static function getInstance() {
>
>     if (!self::$instance)
>      {
>     /*** set this to the correct path and add some error checking ***/
>       include($_SERVER['DOCUMENT_ROOT']."/properties.php");
>       self::$instance = array($a, $b, $c, $d);
>    }
>    return self::$instance;
>  }
>
>  private function __clone(){
>  }
>
> }
> ----------------------------------------------------------
>
> and i'm calling it with
>
> returnConfigParams::getInstance(); (probably wrongly)
>
> The question is how to access the individual elements of the array.

I think you're still misunderstanding the concepts involved.

If you have an array assigned to a variable, you access elements of it with 
[].

$foo = array('a', 'b', 'c');

print $foo[0]; // gives 'a'

$bar = array('a' => 'hello', 'b' => 'world');

print $foo['b']; // gives 'world'

http://us3.php.net/manual/en/language.types.array.php

It doesn't matter if you got the array as a return from a function or method 
or not.  

However, what you're doing here is horribly bastardizing classes and 
singletons. :-)  Generally, a getInstance() is used to return an instance of 
a CLASS, not an array.  

If you just want to access the properties of an object, you use the object 
dereference operator, ->.

$foo = new returnConfigParams();

print $foo->a; // prints whatever is the value of the property var $a

http://us3.php.net/manual/en/language.oop.php

Of course, if you're trying to get database parameter information, then all of 
this is grossly over-engineered.

properties.php:
<?php
$dbsettings['a'] = 'foo';
$dbsettings['b'] = 'bar';
$dbsettings['c'] = 'baz';

index.php (or whatever):
require('properties.php');
And you now have an array $dbsettings, which has the values you need.

Much simpler, and much faster.


-- 
Larry Garfield			AIM: LOLG42
larry@xxxxxxxxxxxxxxxx		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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux