0

I have a model generated as Parent from Yii.

class Parent extends AppModel {}

When I used it as follows, I got an error.

$parent = new Parent();

Fatal error: Cannot use 'Parent' as class name as it is reserved in E:\Customer\Qelasy\Project\QelasySecurity\web\SourceCode\protected\modules\user\models\Parent.php on line 14

As I understand, this is because Parent is a keyword in PHP, and Yii has generated this. Is there any workaround to make it work with Parent without changing the model name into Parents?

TRiG
  • 10,148
  • 7
  • 57
  • 107
dev1234
  • 5,376
  • 15
  • 56
  • 115
  • 1
    Similar to [this question](http://stackoverflow.com/questions/5298507/php-reserved-names-functions)... have a look at the link in the accepted answer – Syjin Nov 28 '13 at 10:16

3 Answers3

3

No. You cannot name a class Parent, period. It's a reserved keyword in the core language. Unless you change the core language, you cannot circumvent this restriction. Since class names aren't case sensitive, what would the following statement do?

public function foo() {
    parent::bar();
}

Therefore: no naming of classes that clash with keywords.

deceze
  • 510,633
  • 85
  • 743
  • 889
2

No, you will have to rename your model to something else. Try and be more specific with your class name. What is this a parent of?

MyParent? UserParent? AppParent? Maybe use a synonym?

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
1

Parent is reserverd php keyword, cannot able use it as class name, use different name ,

 class myclass extends AppModel {}

  $myclass = new myclass();

Here is the link, you can find the list of keywords :http://php.net/manual/en/reserved.php

Krish R
  • 22,583
  • 7
  • 50
  • 59