1,105,402 Community Members

trying to move from procedural to object

Member Avatar
luddite
Newbie Poster
5 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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");
Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

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!

Member Avatar
broj1
Posting Virtuoso
1,579 posts since Jan 2011
Reputation Points: 220 [?]
Q&As Helped to Solve: 219 [?]
Skill Endorsements: 21 [?]
Featured
 
0
 

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"));
Member Avatar
veedeoo
Veteran Poster
1,016 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
0
 

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();
Member Avatar
veedeoo
Veteran Poster
1,016 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
0
 

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)
Member Avatar
luddite
Newbie Poster
5 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 1 Year Ago by veedeoo, cereal and broj1
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article