0

I have the following code:

class db { 
    //database class, connects and closes a database connection
    //some properties and methods are hided (such as host-adres, username...)

    public function connect()
    {
        mysql_connect(//parameters)or die(mysql_error());
    }
}

class ban {
    //ban class, bans an ip, again some methods and properties are hided

    public function banIP()
    {
        //here i want to access the connect function of the class db, 
        //without creating a object.
        //some code
    }
}

Now my question, from inside the method banIP() i need to connect to the database, using the function connect() from class db. But how do I access the connect function?

0

2 Answers 2

6

Declare an object of class db and then access it using that object,

   $object = new db();
   $object->connect();

You cannot access a method without creating any object. You will either have to create an object of the class containing the method (class db) or of the class (class ban) inheriting the class db.

 class ban extends db {
    public function banIP()
   {
     $this->connect(); //this acts as an object.
   }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

I believe he wanted to do it without creating an object.
0

Extend the class you would like to inherit those methods from. So, class ban should extend db.

Comments

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.