3

I'm new to OOP in PHP, is that to seems correct ?

class whatever {

    Function Maths() {
    $this->sql->query($requete);

   $i = 0;

  while($val = mysql_fetch_array($this)) { 
    $tab[i][average] = $val['average'];
    $tab[i][randomData] = $val['sum'];
    $i=$i+1;
    }
        return $tab;
}

I want to access the data contained in the array

$foo = new whatever();
$foo->Maths();
 for ($i, $i <= endOfTheArray; i++) {

    echo Maths->tab[i][average];
    echo Maths->tab[i][randomData];
 }

Thank you ;)

EDIT: i want to output the result of the SQL query as an array, so i can access it from outside the class

1
  • my question is : because i'm new to php OOP, i want to know if that i wrote here is right. It's supposed to output a result of an SQL query to an array to access it outside the class. Commented May 7, 2010 at 20:44

2 Answers 2

5

In the interest of helping you out, here are some modifications. Please hear this, though: a lot of this might not make sense without a good background in PHP or OOP in general. You should look at @webbiedave's link.

class whatever {

  static function maths() {
    $tabs = array();
    $results = $this->sql->query($requete);

    while($val = mysql_fetch_array($this)) { 
      $tabs = $val;
    }

    return $tabs;
}
  1. This fixes syntax errors and logic errors (for instance, the creation of the $results variable to hold the SQL query run).
  2. I made the maths method static, since there's really no need to instantiate a whatever() object with this current example.

Here are some modifications to how this would be used:

$results = whatever::maths();
foreacho ($results as $result) {
  echo $result['average'];
  echo $result['randomData'];
}
  1. Since maths() returns something, you need to store that in a variable; simply calling it, as you did previously, doesn't do anything.
  2. That convoluted for loop can be replaced with a foreach loop.
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, obviously i did a lot of confusing things before. I understand very well all your corrected code. does a static call for Maths() function triggers the __construct of the Whatever class which recieve the database link connection in static ? Thanks for your time ;)
@webbiedave, I shall discover a way to send you points and do that pronto. ;)
@Tristan - not sure I understand your question, but calling a static method inside a class does not call a __construct() function. If you want to do stuff in a constructor (like establish a database link), get rid of the static keyword in front of the method and instantiate the class in the manner you did previously.
perfect, you totally get the question since your answer answers it :D Thanks
5

Please check out PHP OOP basics:

http://www.php.net/manual/en/language.oop5.basic.php

Edit: Thanks for cleaning up the code. Try something along the lines of:

$tabs = array();
while($val = mysql_fetch_assoc($result)) { 
    $tabs[] = $val;
}

And:

$foo = new whatever();
$tabs = $foo->Maths();
for ($tabs as $tab) {
    echo $tab['average'];
    echo $tab['randomData'];
}

http://www.php.net/manual/en/language.oop5.basic.php

6 Comments

ok, i've cleaned up a lot of things. Is that better this way ?
No - there are still several problems. For instance, $foo->Maths(); won't do anything and trying to echo Maths->tab... will throw errors. I agree that it is difficult to discern what you're trying to accomplish - can you tell us exactly what you want to do?
This "answer" should be a comment not an answer.
I want to output the result of the SQL query as an array, so i can access it from outside the class like <p> <?php print average ?></p>
The PHP manual isn't that great in explaining OOP to beginners. Google "PHP classes and OOP". there are some much better sites explaining OOP to newcomers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.