0

I have simple line class autorization extends mysql and it causes Fatal error: Call to a member function close() on a non-object in... On that line is

function __destruct(){
        $this->connection->close();
    }

Constructor

$mysqli = new mysqli(*****);
$this->connection = $mysqli;

at the moment no other methods ot class mysql are not used

tereško
  • 58,060
  • 25
  • 98
  • 150
user1564141
  • 5,911
  • 5
  • 21
  • 18
  • Well, what is `$this->connection`? *Is* it an object? – Pekka Aug 08 '12 at 07:16
  • Seems, that `$this->connection` is not an object (probably `null`), but without seeing, where you define it, or what you do with it in between, nobody can tell you _why_ – KingCrunch Aug 08 '12 at 07:16
  • yes, sorry, forgot about constructor – user1564141 Aug 08 '12 at 07:17
  • Where are you creating that instance of mysqli? In your auth class or your mysql class? And where is that destructor sitting? –  Aug 08 '12 at 07:23
  • 1
    IMO this `autorization` or `authorization` as its speelled has nothing todo with your database and thus should not extend your mysql class but instead have the databse connection passed to it. – Lawrence Cherone Aug 08 '12 at 07:27
  • @navnav all in mysql class, autorization only extends – user1564141 Aug 08 '12 at 07:28
  • But `extends` usually means "is a", i.e. to a certain degree (the more the better) you should be able to put an instance of `autorization` where the code expects an instance of `mysql`. And given the names of the classes imho that's rather unlikely; therefore I agree with Lawrence. see also: https://en.wikipedia.org/wiki/Liskov_substitution_principle – VolkerK Aug 08 '12 at 07:36
  • @LawrenceCherone thanks for authorization. The problem is that i cant directly access authorization (only from other class) – user1564141 Aug 08 '12 at 07:39
  • @VolkerK something wrong with my oop... As i see, the constructor runs automatically when object creates, so when i calling some methods from parent class - it works or not? – user1564141 Aug 08 '12 at 07:51
  • possible duplicate of [Call to a member function on a non-object](http://stackoverflow.com/questions/54566/call-to-a-member-function-on-a-non-object) – tereško Feb 17 '13 at 12:14

2 Answers2

1

Connection property is not initialised . If you have a constructor in mysql class make sure that you haven't completely overwritten it by specifying a constructor in autorization class. This can be achieved by adding:

parent::__constructor( any_params_to_mysql_go_here );

Btw from your question is not clear who maintains the connection property: mysql class or not?

softius
  • 166
  • 5
  • all work with mysql is in the mysql class, which is extended. From authorization(missed one letter in first post) i just call methods with parameters which returns data from db. The problem is that i thought that when i extend class constructor automatically creates new object with connection in extended class, but as is see now i was wrong. – user1564141 Aug 08 '12 at 07:54
0

for testing purposes try

function __destruct(){
    if ( !is_object($this->connection) ) {
        echo ' $this->connection is not an object', "\n";
        var_dump($this->connection);
        die;
    }
    if ( !method_exists($this->connection, 'close') ) {
        echo '$this->connection has no method close()', "\n";
        echo get_class($this->connection), "\n";
        die;
    }
    $this->connection->close();
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • returns $this->connection is not an object NULL... But constructor creates new mysqli connections and i only want to close it. – user1564141 Aug 08 '12 at 07:30