Re: [PHP] Variable Scope from child to parent

2008-05-15 Thread Andrew Ballard
Ok, I forgot to reply-all to the list. Here we go again.

On Wed, May 14, 2008 at 6:33 PM, Tyson Vanover [EMAIL PROTECTED] wrote:
 I am trying to get a child class to pass an array of valid keys to it's
 parent when the constructor is run, and the parent appends the data to one
 of it's array of valid keys.  Then it walks through an array pulling out
 values that have valid keys, and putting them in an array for processing
 later.  I have tried explicitly stating the variable scope.

 abstract class parentclass
 {
  protected $vkeys= array('title1','title2','title3');
  protected $a;

  function __construct($set = NULL, $special = NULL)
  {
self::$this-vkeys= array_merge(self::$this-vkeys, $special);
foreach($set as $key=$value)
{
  if (in_array($key,self::$this-vkeys))
  {
$this-a[$key] = $value;
  }
}
print_r(self::$this-vkeys);  //output below
print_r(self::$this-a);  //output below
  }
 }

 class childclass extends parentclass
 {
  protected $vkeys= array('titleA', 'titleB', 'TitleC');

  function __construct($set, $special = NULL)
  {
parent::__construct($set, self::$this-vkeys);
unset(self::$this-vkeys);
  }
 }

 Unfortunately it seems to duplicate the child's array instead of appending.
  Explicitly stating scope does not seem to help.


 print_r(self::$this-vkeys);
 Array (
  [0] = titleA
  [1] = titleB
  [2] = titleB
  [3] = titleA
  [4] = titleB
  [5] = titleB
 )

 print_r(self::$this-a);
 Array()

 Any thoughts?

A couple. First, as Gabriel said, replace self::$this- with just
$this-. Second, the protected variable $vkeys in the childclass
effectively overwrites the variable of the same name in the parent
class, so it already has the values you are trying to merge
(array('titleA', 'titleB', 'TitleC')) when the childclass is
instantiated before you even call the constructor. That's why the
constructor duplicates the values. Try this  for the constructor for
your childclass:

function __construct($set, $special = array())
{
$merged = array_merge($special, array('titleA', 'titleB', 'TitleC'));
parent::__construct($set, $merged);
}



Andrew


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



[PHP] Variable Scope from child to parent

2008-05-14 Thread Tyson Vanover
I am trying to get a child class to pass an array of valid keys to it's 
parent when the constructor is run, and the parent appends the data to 
one of it's array of valid keys.  Then it walks through an array pulling 
out values that have valid keys, and putting them in an array for 
processing later.  I have tried explicitly stating the variable scope.


abstract class parentclass
{
  protected $vkeys= array('title1','title2','title3');
  protected $a;

  function __construct($set = NULL, $special = NULL)
  {
self::$this-vkeys= array_merge(self::$this-vkeys, $special);
foreach($set as $key=$value)
{
  if (in_array($key,self::$this-vkeys))
  {
$this-a[$key] = $value;
  }
}
print_r(self::$this-vkeys);  //output below
print_r(self::$this-a);  //output below
  }
}

class childclass extends parentclass
{
  protected $vkeys= array('titleA', 'titleB', 'TitleC');

  function __construct($set, $special = NULL)
  {
parent::__construct($set, self::$this-vkeys);
unset(self::$this-vkeys);
  }
}

Unfortunately it seems to duplicate the child's array instead of 
appending.  Explicitly stating scope does not seem to help.



print_r(self::$this-vkeys);
Array (
  [0] = titleA
  [1] = titleB
  [2] = titleB
  [3] = titleA
  [4] = titleB
  [5] = titleB
)

print_r(self::$this-a);
Array()

Any thoughts?

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



Re: [PHP] Variable Scope from child to parent

2008-05-14 Thread Gabriel Sosa
tyson

i never saw
self::$this-vkeys
doing this you actually are callig the var inside $this-vkeys


do  self::$vkeys as static or $this-vkeys as object don't mix


regards

On Wed, May 14, 2008 at 7:33 PM, Tyson Vanover [EMAIL PROTECTED] wrote:
 I am trying to get a child class to pass an array of valid keys to it's
 parent when the constructor is run, and the parent appends the data to one
 of it's array of valid keys.  Then it walks through an array pulling out
 values that have valid keys, and putting them in an array for processing
 later.  I have tried explicitly stating the variable scope.

 abstract class parentclass
 {
  protected $vkeys= array('title1','title2','title3');
  protected $a;

  function __construct($set = NULL, $special = NULL)
  {
self::$this-vkeys= array_merge(self::$this-vkeys, $special);
foreach($set as $key=$value)
{
  if (in_array($key,self::$this-vkeys))
  {
$this-a[$key] = $value;
  }
}
print_r(self::$this-vkeys);  //output below
print_r(self::$this-a);  //output below
  }
 }

 class childclass extends parentclass
 {
  protected $vkeys= array('titleA', 'titleB', 'TitleC');

  function __construct($set, $special = NULL)
  {
parent::__construct($set, self::$this-vkeys);
unset(self::$this-vkeys);
  }
 }

 Unfortunately it seems to duplicate the child's array instead of appending.
  Explicitly stating scope does not seem to help.


 print_r(self::$this-vkeys);
 Array (
  [0] = titleA
  [1] = titleB
  [2] = titleB
  [3] = titleA
  [4] = titleB
  [5] = titleB
 )

 print_r(self::$this-a);
 Array()

 Any thoughts?

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





-- 
Los sabios buscan la sabidurĂ­a; los necios creen haberla encontrado.
Gabriel Sosa

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