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();

            array_push($this->anArray,$data);
            // 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:


<?php

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

    $andy->load_Array("andy","bill");
    $andy->get_element();

    ?>

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 :

$andy->load_Array("andy","bill");

Try:

$andy->load_Array(array("andy","bill"));

right now you are sending two arguments, otherwise use func_get_args(): http://php.net/manual/en/function.func-get-args.php

Also instead of var $element use private|protected|public:

public $element;

bye!

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):

    $andy->load_Array(array("andy","bill"));

Hi,

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.

<?php

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)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.