Jump to content

Recommended Posts

Below is a class that I use to store inofrmation that is in an array. Below in the function display I printing this info. However i want to be able to print this data somewhere else how will I call the function to do this? Each value that is in the array must be created as a variable?

 

Thanks for any help with this.

 

<?
class Order
  {
      var $TestArray = array();    

      function Test($inputArray)
      {
      $this->setTestArray($inputArray);
      $this->Display();
      }

function Display()
      {
$array = $this->TestArray;
$meal1 = $array[0];
echo $meal1."<br>";
$meal2 =  $array[1];
echo $meal2."<br>";
$meal3 = $array[2];
echo $meal3."<br>";
$meal4 = $array[3];
echo $meal4."<br>";
$meal5 = $array[4];
echo $meal5."<br>";
$meal6 = $array[5];
echo $meal6."<br>";
$meal7 = $array[6];
echo $meal7."<br>";
      foreach($this->TestArray as $value)
      {
      
      //echo $value. "<br>";
      }
      }
  
      
  
       
  
      function setTestArray($inputArray)
      {
      if(is_array($inputArray))
      {
      $this->TestArray = $inputArray;
      }
      }
  
       
  
      function echoArrayA1()
      {
      $a = array('a');
      foreach($a as $value)
      {
      //echo "<br />" . $value;
      }
      }
  
       
  
      function echoArrayA2()
      {
      $a[0] = 'b';
      foreach($a as $value)
      {
     // echo "<br />" . $value;
      }
      }
}
  
      ?>

Link to comment
https://forums.phpfreaks.com/topic/133797-array/
Share on other sites

function GetData() {

    return $this->TestArray;

}

 

function SetData($data) {

    $this->TestArray = $data;

}

 

 

 

Then you could use GetData to get and do what ever you want with the array somewhere else.

 

 

No offense, but that's some pretty crappy OOP code.  The syntax is fine, but the actual design of it is very... bad.

 

 

Perhaps look into OOP more in design and practice?

Link to comment
https://forums.phpfreaks.com/topic/133797-array/#findComment-696303
Share on other sites

im trying to learn oop at the moment could you explain the reasons for its poor design and how I can rectify them?

 

When i use this

 $array = $order->getData();

to access getData how will I extract the the different values in the array?

 

Thanks and apologies for the basic and stupid questions

Link to comment
https://forums.phpfreaks.com/topic/133797-array/#findComment-696317
Share on other sites

im trying to learn oop at the moment could you explain the reasons for its poor design and how I can rectify them?

 

When i use this

 $array = $order->getData();

to access getData how will I extract the the different values in the array?

 

Thanks and apologies for the basic and stupid questions

 

 

It wasn't a stupid question.  I wasn't trying to call you or your questino stupid, I was just trying to say that you have the wrong approach.

Link to comment
https://forums.phpfreaks.com/topic/133797-array/#findComment-696360
Share on other sites

Fatal error: Call to a member function getData() on a non-object

 

That simply means that the variable on which you are trying to call the getData method is not an object.

 

 

"Could you help me to get the right approach with the code that i have?"

 

 

Well the only thing really wrong with it is that I don't think it's the Order classes job to handle output.  Shouldn't it be just handling orders, not displaying them?

 

When the actual class handles displaying the data, you run into problems like that one you had.  (Although in this case you could have just modified the function to only set new data if new data were provided.)

Link to comment
https://forums.phpfreaks.com/topic/133797-array/#findComment-696417
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.