Funktion in einer Funktion?

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Funktion in einer Funktion?

    Hallo,
    ich wollte mal fragen ob es möglich ist eine Funktion in einer Funktion zuschreiben.

    Also ich habe es grad einfach mal getestet, und ich bekomme ein Error:
    Fatal error: Call to a member function sendQuery() on a non-object in C:\xampp\htdocs\oop\test.php on line 7


    class.php

    Quellcode

    1. <?php
    2. require("mysql.class.php");
    3. class SLM extends SQL {
    4. function connection($host, $user, $pass, $db) {
    5. SQL::connect($host, $user, $pass, $db);
    6. }
    7. function SQL() {
    8. function sendQuery($dummy) {
    9. SQL::query($dummy);
    10. }
    11. function close() {
    12. SQL::close();
    13. }
    14. function num_rows($dummy) {
    15. SQL::num_rows($dummy);
    16. }
    17. function fetch($dummy) {
    18. SQL::fetch_assoc($dummy);
    19. }
    20. }
    21. }
    22. ?>
    Alles anzeigen


    Und so habe ich die Funktion aufgerufen:

    Quellcode

    1. $test = new SLM();
    2. $test->SQL()->sendQuery("SELECT * FROM sduser");


    Ich wollte halt das so machen damit man mehr Übersicht hat in der Class.php. Deswegen meine Frage geht das überhaupt?
  • klingt für mich leider auch nach Nonsense. Warum nutzt du überhaupt SLM?
    Sieht mir nach einem Pseudonamespace aus. Schau dir mal PHP 5.3 an, das besitzt schon Namespace Support.

    Ansonsten fand ich die Nonsense Idee (nicht persönlich nehmen) gerade ganz lustig und habe das mal umgesetzt:
    Mit der AnyClass kannst du beliebige Klassen als Funktionen ansprechen ;)

    Quellcode

    1. <?php
    2. class A {
    3. public function foo() {
    4. return "ich bin a";
    5. }
    6. }
    7. class AnyClass {
    8. static $loaded = array();
    9. public function __call($method, $args) {
    10. $crc = sha1($method.print_r($args,1));
    11. if(!isset(self::$loaded[$crc])) {
    12. self::$loaded[$crc] = new $method($args);
    13. }
    14. return self::$loaded[$crc];
    15. }
    16. }
    17. $x = new AnyClass();
    18. echo $x->A()->foo();
    Alles anzeigen


    Wenn du dann auch noch von der AnyClass erbst, kannst du beliebige Viele Objekte miteinander verknüpfen.

    Quellcode

    1. class B extends AnyClass {
    2. public function foo() {
    3. return "ich bin b";
    4. }
    5. }
    6. echo $x->B()->B()->A()->foo();
    7. ?>
  • Hi,

    mich wundert auch, dass du die SQL Klasse vererbst und statischen Funktionen benutzt ;)

    Ich kenne die mysql.class nicht, aber ich hätte das so in die Richtung gehend implementiert, wenn du unbedingt alles verketten möchtest ^^

    Quellcode

    1. <?php
    2. require("mysql.class.php");
    3. class SLM extends SQL {
    4. protected $_connection;
    5. private $_query;
    6. function __construct($host, $user, $pass, $db) {
    7. $this->_connection = $this->connect($host, $user, $pass, $db);
    8. }
    9. public function connection($host, $user, $pass, $db){
    10. self::__construct($host, $user, $pass, $db);
    11. return $this;
    12. }
    13. public function getConnection(){
    14. return $this->_connection();
    15. }
    16. public function getQuery() {
    17. return $this->_query;
    18. }
    19. public function setQuery($query){
    20. $this->_query = $query;
    21. return $this;
    22. }
    23. public function sendQuery($query = NULL){
    24. if($query === NULL)
    25. $query = $this->getQuery();
    26. $this->query($query);
    27. return $this;
    28. }
    29. public function close(){
    30. $this->close();
    31. return $this;
    32. }
    33. public function numRows($query = NULL){
    34. if($query === NULL)
    35. $query = $this->getQuery();
    36. return $this->num_rows($query);
    37. }
    38. public function fetch($query = NULL){
    39. if($query === NULL)
    40. $query = $this->getQuery();
    41. return $this->fetch_assoc($query);
    42. }
    43. }
    44. ////////////////// TESTING //////////////
    45. $db = new SLM;
    46. $rowset = $db->connection('host','root','','DB')->setQuery('SELECT * FROM table')->fetch();
    47. ?>
    Alles anzeigen