php - Cannot declare class \Content, because the name is already in use -




i've setup server yii2 , humhub ext,

what i'm trying set rest api , 500 internal server error :

"message": "cannot declare class humhub\modules\content\models\content, because name in use", "file": "***\content\models\content.php", 

the first class :

<?php  namespace app\models;  use yii;  include '../humhub/modules/content/models/content.php'; class content extends \humhub\modules\content\models\content {     public function getposts()     {         return $this->hasmany(post::classname(), ['id' => 'object_id']);     } } 

the second class (pretty big):

<?php  /**  * @link https://www.humhub.org/  * @copyright copyright (c) 2017 humhub gmbh & co. kg  * @license https://www.humhub.com/licences  */  namespace humhub\modules\content\models; include __dir__ .'/../../../modules\content\models\contentdeprecated.php';  use yii; use humhub\modules\user\components\permissionmanager; use yii\base\exception; use yii\helpers\url; use humhub\modules\user\models\user; use humhub\modules\space\models\space; use humhub\modules\content\components\contentactiverecord; use humhub\modules\content\components\contentcontaineractiverecord; use humhub\modules\content\permissions\managecontent; use yii\rbac\permission;  /**  * model class table "content".  *  *  * followings available columns in table 'content':  * @property integer $id  * @property string $guid  * @property string $object_model  * @property integer $object_id  * @property integer $visibility  * @property integer $pinned  * @property string $archived  * @property string $created_at  * @property integer $created_by  * @property string $updated_at  * @property integer $updated_by  * @property contentcontainer $contentcontainer  *  * @since 0.5  */ class content extends contentdeprecated {      /**      * array of user objects should informed new content.      *      * @var array user      */     public $notifyusersofnewcontent = [];      /**      * @var int private visibility mode (e.g. space member content or user profile posts friends)      */     const visibility_private = 0;      /**      * @var int public visibility mode, e.g. content visibile followers      */     const visibility_public = 1;      /**      * @var int owner visibility mode, visible contentcontainer + content owner      */     const visibility_owner = 2;      /**      * @var contentcontaineractiverecord container (e.g. space or user) content belongs to.      */     protected $_container = null;      /**      * @inheritdoc      */     public function behaviors()     {         return [             [                 'class' => \humhub\components\behaviors\polymorphicrelation::classname(),                 'mustbeinstanceof' => array(contentactiverecord::classname()),             ],             [                 'class' => \humhub\components\behaviors\guid::classname(),             ],         ];     }      /**      * @inheritdoc      */     public static function tablename()     {         return 'content';     }      /**      * @inheritdoc      */     public function rules()     {         return [             [['object_id', 'visibility', 'pinned'], 'integer'],             [['archived'], 'safe'],             [['guid'], 'string', 'max' => 45],             [['object_model'], 'string', 'max' => 100],             [['object_model', 'object_id'], 'unique', 'targetattribute' => ['object_model', 'object_id'], 'message' => 'the combination of object model , object id has been taken.'],             [['guid'], 'unique']         ];     }      /**      * returns content object given class , id      *      * @param string $classname class name of content      * @param int $id primary key      */     public static function get($classname, $id)     {         $content = self::findone(['object_model' => $classname, 'object_id' => $id]);         if ($content != null) {             return $classname::findone(['id' => $id]);         }         return null;     }      /**      * @inheritdoc      */     public function beforesave($insert)     {         if ($this->object_model == "" || $this->object_id == "") {             throw new exception("could not save content object_model or object_id!");         }           // set default values         if (!$this->archived) {             $this->archived = 0;         }         if (!$this->visibility) {             $this->visibility = self::visibility_private;         }         if (!$this->pinned) {             $this->pinned = 0;         }          if ($insert) {             if ($this->created_by == "") {                 $this->created_by = yii::$app->user->id;             }         }          $this->stream_sort_date = new \yii\db\expression('now()');          if ($this->created_by == "") {             throw new exception("could not save content without created_by!");         }          return parent::beforesave($insert);     }      /**      * @inheritdoc      */     public function aftersave($insert, $changedattributes)     {         $contentsource = $this->getpolymorphicrelation();          foreach ($this->notifyusersofnewcontent $user) {             $contentsource->follow($user->id);         }          if ($insert && !$contentsource instanceof \humhub\modules\activity\models\activity) {              if ($this->container !== null) {                 $notifyusers = array_merge($this->notifyusersofnewcontent, yii::$app->notification->getfollowers($this));                  \humhub\modules\content\notifications\contentcreated::instance()                         ->from($this->user)                         ->about($contentsource)                         ->sendbulk($notifyusers);                  \humhub\modules\content\activities\contentcreated::instance()                         ->about($contentsource)->save();                   yii::$app->live->send(new \humhub\modules\content\live\newcontent([                     'sguid' => ($this->container instanceof space) ? $this->container->guid : null,                     'uguid' => ($this->container instanceof user) ? $this->container->guid : null,                     'originator' => $this->user->guid,                     'contentcontainerid' => $this->container->contentcontainerrecord->id,                     'visibility' => $this->visibility,                     'contentid' => $this->id                 ]));             }         }          return parent::aftersave($insert, $changedattributes);     }      /**      * @inheritdoc      */     public function afterdelete()     {         // try delete underlying object (post, question, task, ...)         $this->resetpolymorphicrelation();         if ($this->getpolymorphicrelation() !== null) {             $this->getpolymorphicrelation()->delete();         }          parent::afterdelete();     }      /**      * returns visibility of content object      *      * @return integer      */     public function getvisibility()     {         return $this->visibility;     }      /**      * checks if content visiblity set public.      *      * @return boolean      */     public function ispublic()     {         return $this->visibility == self::visibility_public;     }      /**      * checks if content visiblity set private.      *      * @return boolean      */     public function isprivate()     {         return $this->visibility == self::visibility_private;     }      /**      * checks if content object pinned      *      * @return boolean      */     public function ispinned()     {         return ($this->pinned);     }      /**      * pins content object      */     public function pin()     {         $this->pinned = 1;         //this prevents call of beforesave, , setting of update_at         $this->updateattributes(['pinned']);     }      /**      * unpins content object      */     public function unpin()     {          $this->pinned = 0;         $this->updateattributes(['pinned']);     }      /**      * checks if user can pin content.      * allowed workspace owner.      *      * @return boolean      */     public function canpin()     {         if ($this->isarchived()) {             return false;         }          return $this->getcontainer()->permissionmanager->can(new managecontent());     }      /**      * creates list of pinned content objects of wall      *      * @return int      */     public function countpinneditems()     {         return content::find()->where(['content.contentcontainer_id' => $this->contentcontainer_id, 'content.pinned' => 1])->count();     }      /**      * checks if current content object archived      *      * @return boolean      */     public function isarchived()     {         return $this->archived || ($this->getcontainer() !== null && $this->getcontainer()->isarchived());     }      /**      * checks if current user can archive content.      * content owner , workspace admin can archive contents.      *      * @return boolean      */     public function canarchive()     {         // disabled on user profiles, there no stream filter available yet.         if ($this->getcontainer() instanceof user) {             return false;         }          return $this->getcontainer()->permissionmanager->can(new managecontent());     }      /**      * archives content object      */     public function archive()     {         if ($this->canarchive()) {              if ($this->ispinned()) {                 $this->unpin();             }              $this->archived = 1;             if (!$this->save()) {                 throw new exception("could not archive content!" . print_r($this->geterrors(), 1));             }         }     }      /**      * unarchives content object      */     public function unarchive()     {         if ($this->canarchive()) {              $this->archived = 0;             $this->save();         }     }      /**      * returns url of content.      *      * default returns url of wall entry.      *      * optionally it's possible create own geturl method in underlying      * hactiverecordcontent (e.g. post) overwrite behavior.      * e.g. in case there no wall entry available content.      *      * @since 0.11.1      */     public function geturl()     {         if (method_exists($this->getpolymorphicrelation(), 'geturl')) {             return $this->getpolymorphicrelation()->geturl();         }          return url::toroute(['/content/perma', 'id' => $this->id]);     }      /**      * sets container (e.g. space or user record) content.      *      * @param contentcontaineractiverecord $container      * @throws exception      */     public function setcontainer(contentcontaineractiverecord $container)     {         $this->contentcontainer_id = $container->contentcontainerrecord->id;         $this->_container = $container;     }      /**      * returns content container (e.g. space or user record) of content      *      * @return contentcontaineractiverecord      * @throws exception      */     public function getcontainer()     {         if ($this->_container != null) {             return $this->_container;         }          if ($this->contentcontainer !== null) {             $this->_container = $this->contentcontainer->getpolymorphicrelation();         }          return $this->_container;     }      /**      * relation contentcontainer model      * note: not space or user instance!      *      * @since 1.1      * @return \yii\db\activequery      */     public function getcontentcontainer()     {         return $this->hasone(contentcontainer::classname(), ['id' => 'contentcontainer_id']);     }      /**      * checks if given user can edit content.      *      * user can edit content if 1 of following conditions met:      *      *  - user owner of content      *  - user system administrator , content module setting `admincaneditallcontent` set true (default)      *  - user granted managepermission set model record class      *  - user meets additional condition implemented model records class own `canedit()` function.      *      * @since 1.1      * @param user $user      * @return bool can edit content      */     public function canedit($user = null)     {         if (yii::$app->user->isguest) {             return false;         }          if ($user === null) {             $user = yii::$app->user->getidentity();         }          // owner can edit content         if ($user !== null && $this->created_by == $user->id) {             return true;         }          // global admin can edit/delete arbitrarily content         if (yii::$app->getmodule('content')->admincaneditallcontent && $user->issystemadmin()) {             return true;         }          /* @var $model contentactiverecord */         $model = $this->getpolymorphicrelation();          // check additional manage permission given container         if ($model->hasmanagepermission() && $this->getcontainer() && $this->getcontainer()->getpermissionmanager($user)->can($model->getmanagepermission())) {             return true;         }          // check if underlying models canedit implementation         // todo: implement interface         if (method_exists($model, 'canedit') && $model->canedit($user)) {             return true;         }          return false;     }      /**      * checks given $permission of current user in contents content container.      * short `$this->getcontainer()->getpermissionmanager()->can()`.      *      * @param $permission      * @param array $params      * @param bool $allowcaching      * @see permissionmanager::can()      * @since 1.2.1      * @return bool      */     public function can($permission, $params = [], $allowcaching = true)     {         return $this->getcontainer()->getpermissionmanager()->can($permission, $params, $allowcaching);     }      /**      * checks if user can view content.      *      * @since 1.1      * @param user $user      * @return boolean can view content      */     public function canview($user = null)     {         if (!$user && !yii::$app->user->isguest) {             $user = yii::$app->user->getidentity();         }          // check guest visibility         if (!$user) {             return $this->checkguestaccess();         }          // public visible content         if ($this->ispublic()) {             return true;         }          // check system admin can see content module configuration         if ($user->issystemadmin() && yii::$app->getmodule('content')->admincanviewallcontent) {             return true;         }          if ($this->isprivate() && $this->getcontainer()->canaccessprivatecontent($user)) {             return true;         }          return false;     }      /**      * determines if guest user able read content.      * case if of following conditions met:      *      *  - content public      *  - `auth.allowguestaccess` module setting enabled      *  - space or profile visibility set visibility_all      *      * @return bool      */     public function checkguestaccess()     {         if(!$this->ispublic() || !yii::$app->getmodule('user')->settings->get('auth.allowguestaccess')) {             return false;         }          // check container visibility guests         return ($this->container instanceof space && $this->container->visibility == space::visibility_all)             || ($this->container instanceof user && $this->container->visibility == user::visibility_all);     }      /**      * updates wall/stream sorting time of content "updated at" sorting      */     public function updatestreamsorttime()     {         $this->updateattributes(['stream_sort_date' => new \yii\db\expression('now()')]);     }  } 

as can see first class extends second 1 , i'm rather new php i'm guessing that's issue

update : tried using "use class " , still same error , tried changing name of first class content1 check , still same .

the problem occours in second class error report

this recurring error in few places in server figure solve here , rest same ,

many thanks!

you included file exacly same class name, have 2 class declarations name content in file. easiest ways fix it:

  1. change 1 of class name
  2. start using yii2 autloader, if need include - there's wrong in code. should able access class using namespace. there's small conditions have met:
    • each class must under namespace (e.g. foo\bar\myclass)
    • each class must saved in individual file path determined following algorithm:

// $classname qualified class name without leading backslash $classfile = yii::getalias('@' . str_replace('\\', '/', $classname) . '.php');

more yii2 autloading: class autoloading





wiki

Comments

Popular posts from this blog

elasticsearch - what is the equivalent data type for geo_point in hibernate search? -

firebase - How to wait value in Ionic 2 -

Jenkins: find build number for git commit -