Jump to content


Photo

Error non-static en un método en PHP


  • Please log in to reply
No replies to this topic

#1 gatoher

gatoher

    Newbie

  • Miembros
  • Pip
  • 1 posts

Posted 23 July 2013 - 12:02 PM

Hola a todos, tengo un problema con un script. Es para recuperar los datos de una base de datos y mostrarlo en una web. El script funciona, se ejecuta bien pero me da el siguiente error:

Strict standards: Non-static method dataobject::connect() should not be called statically in C:\wamp\www\verano\bases\book_club\member.class.php on line 39
Strict standards: Non-static method dataobject::disconnect() should not be called statically in C:\wamp\www\verano\bases\book_club\member.class.php on line 58

Lo de que no debería ser llamado el método static no lo entiendo, He comentado los metodos que son ,el script es este:



delphi
  1. <?php
  2.  
  3. require_once "dataobject.class.php";
  4.  
  5. class member extends dataobject {
  6.  
  7. protected $data = array (
  8.  
  9. "id" => "",
  10. "username" => "",
  11. "password" => "",
  12. "firstname" => "",
  13. "lastname" => "",
  14. "joindate" => "",
  15. "gender" => "",
  16. "favoritegenre" =>"",
  17. "emailaddress" => "",
  18. "otherinterests" => ""
  19.  
  20. );
  21.  
  22.  
  23. private $_genres = array (
  24.  
  25. "crime" => "Crime",
  26. "horror" => "Horror",
  27. "thriller" => "Thriller",
  28. "romance" => "Romance",
  29. "scifi" => "Sci-fi",
  30. "adventure" => "Adventure",
  31. "nonfiction" => "Non-Fiction"
  32.  
  33.  
  34. );
  35.  
  36.                         // Este es el que da problemas
  37. public static function getmembers ($startrow, $numrows, $order) {
  38.  
  39. $conn = parent::connect ();
  40. $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " ORDER BY $order LIMIT :startrow, :numrows";
  41.  
  42.  
  43.  
  44. try {
  45.  
  46. $st = $conn->prepare($sql);
  47. $st -> bindvalue (":startrow", $startrow, PDO::PARAM_INT);
  48. $st -> bindvalue (":numrows", $numrows, PDO::PARAM_INT);
  49. $st -> execute ();
  50. $members = array ();
  51. foreach ($st -> fetchall () as $row){
  52.  
  53. $members [] = new member ($row);
  54. }
  55.  
  56. $st = $conn ->query ("SELECT found_rows () as totalrows");
  57. $row = $st -> fetch ();
  58. parent::disconnect ($conn);
  59. return array ($members, $row["totalrows"]);
  60.  
  61.  
  62.     } catch (PDOException $e) {
  63.  
  64. parent::disconnect ($conn);
  65. die ("Query failed : " . $e->getmessage());    
  66.  
  67.  
  68.  
  69.     }
  70.  
  71.  
  72.  
  73. }
  74.  
  75.  
  76.                                       // Este es el otro
  77. public static function getmember ($id) {
  78.  
  79. $conn = parent::connect ();
  80. $sql = "SELECT * FROM " . TBL_MEMBERS . " WHERE id = :id";
  81.  
  82. try {
  83.  
  84. $st = $conn->prepare ($sql);
  85. $st ->bindvalue (":id", $id, PDO::PARAM_INT);
  86. $st ->execute ();
  87. $row = $st->fetch ();
  88. parent::disconnect ($conn);
  89. if ($row) return new member ($row);
  90.  
  91. } catch (PDOException $e){
  92.  
  93. parent::disconnect ($conn);
  94. die ("Query failed: " . $e->getmessage ());
  95. }
  96.  
  97.  
  98. }
  99.  
  100. public function getgenderstring () {
  101.  
  102. return ( $this->data["gender"]  == "f" )  ? "Female" : "Male";
  103. }
  104.  
  105. public function getfavoritegenrestring () {
  106.  
  107. return ( $this->_genres[$this->data["favoritegenre"]]) ;
  108. }
  109.  
  110.  
  111. }



Como veis apunta a una clase abstracta que esta en otro archivo, es este:



delphi
  1. <?php
  2.  
  3. require_once "config.php";
  4. // Esta la clase a la que apunta
  5. abstract class dataobject {
  6.  
  7. protected $data = array ();
  8.  
  9. public function __construct ($data) {
  10. foreach ($data as $key => $value){
  11. if (array_key_exists ($key, $this->data) ) $this-> data[$key] =$value;
  12. }
  13.  
  14. }
  15.  
  16. public function getvalue ($field) {
  17. if (array_key_exists ($field, $this->data) ) {
  18. return $this->data[$field];
  19.  
  20. } else {
  21. die  ("Field not found");
  22.  
  23. }
  24. }
  25.  
  26.  
  27. public function getvalueencoded ($field) {
  28.  
  29. return htmlspecialchars ($this->getvalue($field) );
  30.  
  31. }
  32.  
  33. protected function connect () {
  34.  
  35. try {
  36.  
  37. $conn = new PDO (DB_DSN, DB_USERNAME, DB_PASSWORD);
  38. $conn -> setattribute (PDO::ATTR_PERSISTENT, true);
  39. $conn -> setattribute (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  40.  
  41.       } catch (PDOException $e) {
  42. die ("Connection failed: " . $e->getmessage () );      
  43.       }
  44.  
  45. return $conn;
  46.  
  47. }
  48.  
  49. protected function disconnect ($conn) {
  50.  
  51. $conn  = "";
  52.  
  53. }
  54. }
  55.  
  56.  
  57. ?>



Claro viendo el error, he probado a quitar static de los métodos, pero me sigue devolviendo el mismo fallo.


  • 0




IP.Board spam blocked by CleanTalk.