I'm playing with php code to move from procedural to objects: I created a class called data, in a file called
data2.php and used this in useData.php where I used some procedural and some use of data class.

data class as follows:


class data  


     var $anArray; 
    var $element;

        public  function load_Array($data)

            $this->anArray =   array();

            // data from loading object is $andy->load_Array("andy","bill");
            //only the first entry "andy" is getting loaded

            $this->element =$this->anArray[0]; 


                function get_element()


                          echo  "the output using function is:   ". $this->element;




            then used this class in useData.php as follows:


    $still = array("andy","bill","sid");
    echo "the elements in array using procedural is:   ".$still[0]." and  ".$still[1]."<br>";
    $andy = new data();



The output using useData.php in browser being :

  the elements in array using procedural is: andy and bill
  the output using function is: andy

On this line :

 public function load_Array($data) 

it seems that only "andy" is getting loaded into array, since if I put a "1" instead of "0" in this line

$this->element =$this->anArray[0]; 

        i get an error I guess because there is no second element in the array. 

why isn't the second element of "bill" getting loaded into array using this line :

4 Years
Discussion Span
Last Post by luddite

You might be better off declaring the method this way:

public  function load_Array($data)
    $this->anArray = array();

    foreach($data as $element) {
        $this->anArr[] = $element;

Now you can pass an array as argument (as cereal suggested):


Edited by broj1



here is a simple class pretty much similar to what you have above. $element is not mandatory during the instantiation. However, the $array is mandatory.


class ThisArray{

public $element, $array;

public function __construct($array=array(),$element=null){
$this->element = $element;
$this->array = $array;


public function load_Array(){

return $this->array;


public function get_element(){

return $this->element;

$element = 'Hello this is element';
$nameArray = array('bob','bill','rich','poorboy');

$andy = new ThisArray($nameArray,$element);

$arrayres = $andy->load_Array();
echo $arrayres[0];
echo '<br/>';
echo $arrayres[1];
echo '<br/>';
echo $arrayres[3];

echo '<br/>';
echo $andy->get_element();

this will work also, even if the $element is not initialized

$andy = new ThisArray($nameArray);

because of the the =NULL in the constructor..

You can also access the object similar to what broj1 have suggested.. something like this

foreach($arrayres as $name){
echo $name.'<br/>';


If this one, looks odd to you.

public function __construct($array = array(),$element=null)

you can also change it to this to make it a lot nicer

public function __construct(array $array,$element=null)

Edited by veedeoo: info added


Many thanks for all the help, much appreciated, read all posts; some are going to take a few hours to sink in!

so far tried the func_get_args, which is the simplest to understand, it returns an array so just used it like so:

public  function load_Array($data)

                  $this->anArray =   func_get_args($data);

            //func_get_args    above reurns an array

            $this->element = $this->anArray[0]; 
            $this->element2 = $this->anArray[1];
            $this->element3 = $this->anArray[2];


i can see that if I have less than three arguments in $data

then I might get a warning on last line

$this->element3 = $this->anArray[2];  
//since [2] won't be there

But this code is only for play, so this wouldn't be an issue

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.