sebap123
sebap123

Reputation: 2685

Using variable of extended class in PHP

I guess this might be quite easy question but I cannot find any solution, so I hope someone will be able to give me a hand.

I am JAVA developer currently trying to learn OOP in PHP writing in NetBeans. In this IDE there is this functionality of code auto completion/suggestions. However, in my classes I am unable to use it.

I have two files with classes:

abstract class DB {

    protected $db;

    public function __construct() {
        try {
            require_once '../../MysqliDb.php';
            $this->db = new MysqliDb(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        } catch (Exception $e) {
            exit('Database connection could not be established.');
        }
    }
}

Class B

include './DB.php';

    class B extends DB{

        public function getRecords() {
            $this->db->//??
        }
    }

In this place marked with question marks I was expecting to get suggestions about methods in $db object, but I don't have them at all.

Can someone tell me what I have to do to have those suggestions there? I am thinking that I don't have them there because of some code error, but also it can just be IDE error.

Upvotes: 1

Views: 68

Answers (2)

Yaron U.
Yaron U.

Reputation: 7881

As opposed to Java, PHP is a lose type language - which means that vars and properties of classes don't have a type declaration

What IDE's usually do in order to give a better auto completion capabilities is reading comments (more precisely PHPDOC comments - which is very similar to JAVADOC) to enable such functionality

in NETBEANS (and in Eclipse) it is done by

abstract class DB {
    /**
    * @var MysqliDb
    */
    protected $db;

    ...
}

Upvotes: 3

Ivan Yonkov
Ivan Yonkov

Reputation: 7034

This hints you can achieve by telling the IDE what type the variable is

/**
* @var MysqliDb
*/
protected $db;

Or if you have method that returns it, you should set its return type

/**
* @return MysqliDb
*/
protected function getDb() {
    return $this->db;
}

Upvotes: 2

Related Questions