classes and array attributes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stef31
Forum Newbie
Posts: 1
Joined: Mon Jul 04, 2005 8:57 am

classes and array attributes

Post by stef31 »

Hi,

Newbie question I guess. I'd like to declare an array as an attribute of a class. Everytime I try to use the declared array, I have a warning saying the attribute is not an array, and the result is of course wrong.

For example

Code: Select all

<?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this->f1($my_array) ;
  }
}

?>
Calling f2 will just say that array_rand expects an array as first parameter - which I know, and $my_array IS an array...

What's wrong ?

Thanks,

d11wtq | If you're using

Code: Select all

tags don't disable BBCode... that's just silly  [/color]urse wrong.

For example

Code: Select all

<?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this-&gt;f1($my_array) ;
  }
}

?&gt;
Calling f2 will just say that array_rand expects an array as first parameter - which I know, and $my_array IS an array...

What's wrong ?

Thanks,

d11wtq | If you're using

Code: Select all

tags don't disable BBCode... that's just silly  [/color]gs don't disable BBCode... that's just silly  [/color]bute of a class. Everytime I try to use the declared array, I have a warning saying the attribute is not an array, and the result is of course wrong.

For example

Code: Select all

<?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this->f1($my_array) ;
  }
}

?>
Calling f2 will just say that array_rand expects an array as first parameter - which I know, and $my_array IS an array...

What's wrong ?

Thanks,

d11wtq | If you're using

Code: Select all

tags don't disable BBCode... that's just silly  [/color] warning saying the attribute is not an array, and the result is of course wrong.

For example

Code: Select all

&lt;?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this-&gt;f1($my_array) ;
  }
}

?&gt;
Calling f2 will just say that array_rand expects an array as first parameter - which I know, and $my_array IS an an attribute of a class. Everytime I try to use the declared array, I have a warning saying the attribute is not an array, and the result is of course wrong.

For example

Code: Select all

<?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this->f1($my_array) ;
  }
}

?>
Calling f2 will justion I guess. I'd like to declare an array as an attribute of a class. Everytime I try to use the declared array, I have a warning saying the attribute is not an array, and the result is of course wrong.

For example

Code: Select all

<?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this->f1($my_array) ;
  }
}

?>
Calling f2 will just say that array_rand expects an array as first parameter - which I know, and $my_array IS an array...

What's wrong ?

Thanks,

d11wtq | If you're using

Code: Select all

tags don't disable BBCode... that's just silly  [/color]tribute of a class. Everytime I try to use the declared array, I have a warning saying the attribute is not an array, and the result is of course wrong.

For example

Code: Select all

&lt;?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this-&gt;f1($my_array) ;
  }
}

?&gt;
Calling f2 will just say that array_rand expects an a to declare an array as an attribute of a class. Everytime I try to use the declared array, I have a warning saying the attribute is not an array, and the result is of course wrong.

For example

Code: Select all

<?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this->f1($my_array) ;
  }
}

?>
Calling f2 will just say that array_rand expects an array as first parameter - which I know, and $my_array IS an array...

What's wrong ?

Thanks,

d11wtq | If you're using

Code: Select all

tags don't disable BBCode... that's just silly  [/color]tion I guess. I'd like to declare an array as an attribute of a class. Everytime I try to use the declared array, I have a warning saying the attribute is not an array, and the result is of course wrong.

For example

Code: Select all

&lt;?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this-&gt;f1($my_array) ;
  }
}

?&gt;
Calling f2 will just say that array_rand expects an array as first parameter - which I know, and $my_array IS an array...

What's wrong ?

Thanks,

d11wtq | If you're using

Code: Select all

tags don't disable BBCode... that's just silly  [/color:1a
<?php
class ACLass
{
  var $my_array = array() ;

  function f1($p)
  {
    return array_rand($p) ;
  }

  function f2()
  {
    return $this->f1($my_array) ;
  }
}

?>
Calling f2 will just say that array_rand expects an array as first parameter - which I know, and $my_array IS an array...

What's wrong ?

Thanks,

d11wtq | If you're using

Code: Select all

tags don't disable BBCode... that's just silly  [/color]
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Code: Select all

<?php
class ACLass {
  var $my_array;
  function ACLass ( ) { $this->$my_array = array(); }
  function f1( $p ) { return array_rand( $p ); }
  function f2() { return $this->f1($my_array); }
}
?>
It may have been that you need a constructor to intitiate a variable into an array.rn $this->f1($my_array); }
}
?>


It may have been that you need a constructor to intitiate a variable into an array. function ACLass ( ) { $this->$my_array = array(); }
function f1( $p ) { return array_rand( $p ); }
function f2() { return $this->f1($my_array); }
}
?>


It may have been that you need a constructor to intitiate a variable into an array.function f1( $p ) { return array_rand( $p ); }
function f2() { return $this-&amparray;
function ACLass ( ) { $this->$my_array = array(); }
function f1( $p ) { return array_rand( $p ); }
function f2() { return $this->f1($my_array); }
}
?>


It may have been that you need a constructor to intitiate a variable into an array.
<?php
class ACLass {
var $my_array;
function ACLass ( ) { $this->$my_array = array(); }
function f1( $p ) { return array_rand( $p ); }
function f2() { return $this->f1($my_array); }
}
?>


It may have been that you need a constructor to intitiate a variable into an array.;
function ACLass ( ) { $this->$my_array = array(); }
function f1( $p ) { return array_rand( $p ); }
function f2() { return $this-&gt;f1($my_array); }
}
?&gass ACLass {
var $my_array;
function ACLass ( ) { $this->$my_array = array(); }
function f1( $p ) { return array_rand( $p ); }
function f2() { return $this->f1($my_array); }
}
?>


It may have been that you need a constructor to intitiate a variable into an array.
<?php
class ACLass {
var $my_array;
function ACLass ( ) { $this->$my_array = array(); }
function f1( $p ) { return array_rand( $p ); }
function f2() { return $this->f1($my_array); }
}
?>


It may have been that you need a constructor to intitiate a variable into an array.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Almost there... ;)

Code: Select all

<?php
class ACLass 
{
  var $my_array;

  function f1($p) 
  { 
    return array_rand($p); 
  }

  function f2() 
  { 
    return $this->f1($this->my_array); 
  }
}
?>
;f1($this->my_array);
}
}
?>
{
var $my_array;

function f1($p)
{
return array_rand($p);
}

function f2()
{
return $this->f1($this->my_array);
}
}
?>and($p);
}

function f2()
{
return $this->f1($this-&amACLass
{
var $my_array;

function f1($p)
{
return array_rand($p);
}

function f2()
{
return $this-&gt;f1($this->my_array);
}
}
?>

Code: Select all

<?php
class ACLass 
{
  var $my_array;

  function f1($p) 
  { 
    return array_rand($p); 
  }

  function f2() 
  { 
    return $this->f1($this->my_array); 
  }
}
?>
s
{
var $my_array;

function f1($p)
{
return array_rand($p);
}

function f2()
{
return $this-&gt;f1($this-&gt;my_array);
}
}
?&<?php
class ACLass
{
var $my_array;

function f1($p)
{
return array_rand($p);
}

function f2()
{
return $this->f1($this->my_array);
}
}
?>
Post Reply