简体   繁体   中英

PHP object-oriented access to a method and issues with $this

I have been working with Yii framework for a while, but know I am trying to work on my own minimalistic framework based on MVC architecture. Let's just say that I have a parent model ModelCore which extends all other models (mentioned PageModel also). It has defined method:

public function find( $condition ){
    $sql = "SELECT * FROM {$this->table()} WHERE {$condition} LIMIT 1";
    // executing query and returning the result
}

And I am stuck at the point when I want to call this method from other class ( URLresolver ) this way:

//...
elseif ( PageModel::find("`url` = '{$bit}'") != NULL ) {
//...

The scripts ends with fatal error: Call to undefined method URLresolver::table()

If someone can explain me how theese things work in PHP and how can I easily access a method I would be grateful.

Thanks a lot.

If you use :: it refers to a static function. If you created an object, you have to do it like:

$obj = new PageModel( );
$obj->find(" YOUR QUERY ");

public function find( $q ){ }

If you have a static method (in these functions $this does not reffer to the object, because you did not create a new object.

PageModel::find( $q ){}

public static function find ( $q ){ }

The scripts ends with fatal error: Call to undefined method URLresolver::table()

Find where you are calling table() function which seems to dont exist,and... is it a static method? Otherway you dont need the :: notation, but ->

The error is pretty self-explanatory: the URLresolver class doesn't implement (or inherit) a method named table . Are you sure you didn't mean to call a property with that name?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM