Re: [PHP] Retrieving values from array on a class

2006-10-16 Thread Richard Lynch
On Sat, October 14, 2006 5:18 pm, Kevin Waterson wrote:
class returnConfigParams
 {

  var $a;

  function getMySQLParams()
   {
include($_SERVER['DOCUMENT_ROOT']./properties.php);

$values = array(0 = $a, 1 = $b, 2 = $c, 3 = $d);

You probably want $this-a instead of $a

And that also assumes you are using $this-a inside of properties.php...

But I don't think you can even *DO* an include() inside a class
definition, so that should be giving you an error...

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-16 Thread Stut

Richard Lynch wrote:

But I don't think you can even *DO* an include() inside a class
definition, so that should be giving you an error...


You can do an include/require anywhere. However, you cannot declare new 
functions inside an include and use it to add methods or variables to a 
class.


inc.php
?php
function foo()
{
return 'I pity to foo';
}
---

use.php
?php
class bar
{
include('inc.php');
}
$b = new bar();
print $b-foo(); // This will not work
print foo(); // But this will
---

This will not work! foo() is actually defined at the global scope.

-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-15 Thread AR
Hi,

 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'
I know that.

I had my class written as:

 class returnConfigParams

 {

  var $a;
  var $b;
  var $c;
  var $d;


  // function that get the database parameters from properties.php
  function getMySQLParams()

   {

include($_SERVER['DOCUMENT_ROOT']./properties.php);

$mysql_parameters = array(0 = $a, 1 = $b, 2 = $c, 3 = $d);

return($mysql_parameters);

  }

}

?


and i got the array values with:
$params_file = New returnConfigParams;
$params = $params_file-getMySQLParams();
values were $params[0], etc...
everything was fine.


then, someone suggested i might write it like this, so i can call it
with returnConfigParams::getMySQLParams();


 class returnConfigParams

 {

  private static $instance = NULL;

  private function __construct() {
  } 

  // function that get the database parameters from properties.php
  public static function getMySQLParams() {

  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(){
 }  

}

?


This way i can't get the array elements.
I've tried
$params = returnConfigParams::getMySQLParams();
but no good.

And that's the story.

Cheers,
AR

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-15 Thread Larry Garfield
On Sunday 15 October 2006 03:19, AR wrote:
 Hi,

  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'

 I know that.

 I had my class written as:
 
  class returnConfigParams

  {

   var $a;
   var $b;
   var $c;
   var $d;


   // function that get the database parameters from properties.php
   function getMySQLParams()

{

 include($_SERVER['DOCUMENT_ROOT']./properties.php);

 $mysql_parameters = array(0 = $a, 1 = $b, 2 = $c, 3 = $d);

 return($mysql_parameters);

   }

 }

 ?
 

 and i got the array values with:
 $params_file = New returnConfigParams;
 $params = $params_file-getMySQLParams();
 values were $params[0], etc...
 everything was fine.


 then, someone suggested i might write it like this, so i can call it
 with returnConfigParams::getMySQLParams();

 
  class returnConfigParams

  {

   private static $instance = NULL;

   private function __construct() {
   }

   // function that get the database parameters from properties.php
   public static function getMySQLParams() {

   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(){
  }

 }

 ?
 

 This way i can't get the array elements.
 I've tried
 $params = returnConfigParams::getMySQLParams();
 but no good.

 And that's the story.

 Cheers,
 AR

What exactly does properties.php do/contain?

You are aware that the $a, $b, $c, and $d you reference in both versions are 
not the properties of the object, but local variables, right?  If you want 
the object properties, you need to use $this-a, $this-b, etc.

I still think both methods are highly over-engineered, however.

-- 
Larry Garfield  AIM: LOLG42
[EMAIL PROTECTED]   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



[PHP] Retrieving values from array on a class

2006-10-14 Thread AR
Hello,

Sorry for the newbie question :(

I have this class named returnConfigParams that returns an array
called $values through a function called getMySQLParams().

My question is how to retrieve the values of the array from the file
that calls the class.

I have:

$params_file = New returnConfigParams;
$params_file-getMySQLParams();
print($params_file[0]);

but doesn't work :(

Help me please.

I'm stuck on this for two hours and didn't find nothing on Google that
could help me.

Cheers,
AR

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-14 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2006-10-14 15:55:57 +0100:
 Hello,
 
 Sorry for the newbie question :(
 
 I have this class named returnConfigParams that returns an array
 called $values through a function called getMySQLParams().
 
 My question is how to retrieve the values of the array from the file
 that calls the class.
 
 I have:
 
 $params_file = New returnConfigParams;
 $params_file-getMySQLParams();
 print($params_file[0]);

var_dump($params_file-getMySQLParams());

You said the method returns the data, right?

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-14 Thread AR
Hello,

 I have this class named returnConfigParams that returns an array
 called $values through a function called getMySQLParams().

 My question is how to retrieve the values of the array from the file
 that calls the class.

 I have:

 $params_file = New returnConfigParams;
 $params_file-getMySQLParams();
 print($params_file[0]);
 
 var_dump($params_file-getMySQLParams());
 
 You said the method returns the data, right?
Yes, it returns an array called $values.
My problem is how to access the individual member of the array.

Thanks in advance.

Cheers,
AR

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-14 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2006-10-14 16:28:57 +0100:
 Hello,
 
  I have this class named returnConfigParams that returns an array
  called $values through a function called getMySQLParams().
 
  My question is how to retrieve the values of the array from the file
  that calls the class.
 
  I have:
 
  $params_file = New returnConfigParams;
  $params_file-getMySQLParams();
  print($params_file[0]);
  
  var_dump($params_file-getMySQLParams());
  
  You said the method returns the data, right?
 Yes, it returns an array called $values.
 My problem is how to access the individual member of the array.

Just like you'd access members of any other array.

$whatever = $params_file-getMySQLParams();
print($whatever[0]);

This is a matter of basic rules of the language. Did you know
most of the grammer is described in the manual?

http://www.php.net/manual/en/langref.php 

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-14 Thread AR
Hello,

 Just like you'd access members of any other array.
 
 $whatever = $params_file-getMySQLParams();
 print($whatever[0]);
 
 This is a matter of basic rules of the language. Did you know
 most of the grammer is described in the manual?
Sure, i already tried that, but it doesn't works.
All i get is a blank page.

BTW, here is my class:

class returnConfigParams
 {

  var $a;
  var $b;
  var $c;
  var $d;


  function getMySQLParams()
   {
include($_SERVER['DOCUMENT_ROOT']./properties.php);

$values = array(0 = $a, 1 = $b, 2 = $c, 3 = $d);

return($values);

  }

}

Cheers,
AR

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-14 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2006-10-14 16:42:37 +0100:
  Just like you'd access members of any other array.
  
  $whatever = $params_file-getMySQLParams();
  print($whatever[0]);
  
  This is a matter of basic rules of the language. Did you know
  most of the grammer is described in the manual?

 Sure, i already tried that, but it doesn't works.
 All i get is a blank page.
 
 BTW, here is my class:
 
 class returnConfigParams
  {
 
   var $a;
   var $b;
   var $c;
   var $d;
   
 
   function getMySQLParams()
{
 include($_SERVER['DOCUMENT_ROOT']./properties.php);
   
 $values = array(0 = $a, 1 = $b, 2 = $c, 3 = $d);
   
 return($values);
 
   }
 
 }

since you don't say what's in the included file, I'll assume
it's empty. $a will be unset and print($rCP-getMySQLParams())
will print nothing ().

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-14 Thread Kevin Waterson

This one time, at band camp, Roman Neuhauser wrote:


BTW, here is my class:

class returnConfigParams
{

 var $a;
 var $b;
 var $c;
 var $d;


 function getMySQLParams()
  {
   include($_SERVER['DOCUMENT_ROOT']./properties.php);

   $values = array(0 = $a, 1 = $b, 2 = $c, 3 = $d);

   return($values);

 }

}


Why not a static method?
Create a config singleton and you can have an instance of your class
available anywhere by simply using

config::getInstance()


class config{

/*** Declare instance ***/
private static $instance = NULL;

/**
*
* the constructor is set to private so
* so nobody can create a new instance using new
*
*/
private function __construct() {
  /*** nothing to see, move along ***/
}

/**
*
* Return an array instance
*
* @access public
*
* @return array
*
*/
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);
}
return self::$instance;
}

/**
*
* Like the constructor, we make __clone private
* so nobody can clone the instance
*
*/
private function __clone(){
}

} /*** end of class ***/



Just a thought
Kevin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-14 Thread AR
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.

Can someone help me please ?

Cheers,
AR

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Retrieving values from array on a class

2006-10-14 Thread Larry Garfield
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
[EMAIL PROTECTED]   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