Checks if the class method exists in PHP
in PHP coding, we usually need check if the class method exist, if we direct call without check we could get exception. In Yii 2.0 framework, we can also use this way to check model's relation exist.
{ Check Class Method exist }
<?php
namespace YiiLib;
class Topic {
function addComment() { }
}
$topic = new Topic();
var_dump(method_exists($topic, 'addComment')); //true
var_dump(method_exists($topic, 'deleteComment')); //false
{ Yii 2.0 Check Model's Relation exist }
<?php
namespace YiiLib;
class Topic extends \common\components\CommonBased
{
/**
* @return \yii\db\ActiveQuery
*/
public function getComments()
{
return $this->hasMany(Comment::className(), ['com_topic_id' => 't_id']);
}
$topic = new Topic();
if(method_exists($topic, 'getComments')){
//TODO relation exist
}
Leave Comment