草庐IT

Inheritance

全部标签

php - 覆盖静态变量

我有两个类(模型和用户)但是我有一个问题所以我试着用一个简单的例子来解释它:classperson{protectedstatic$todo="nothing";publicfunction__construct(){}publicfunctionget_what_todo(){echoself::$todo;}}classstudentextendsperson{protectedstatic$todo="studing";}$s=newstudent();$s->get_what_todo();//thiswillshowtheword(nothing)//butIwanttosh

php - 继承php中的属性

我有一个父类(superclass),其中包含用于设置它们的属性和方法classSuper{private$property;function__construct($set){$this->property=$set;}}然后我有一个子类需要使用那个属性classSubextendsSuper{private$sub_propertyfunction__construct(){parent::__construct();$this->sub_property=$this->property;}}但是我总是报错Notice:Undefinedproperty:Sub::$propert

PHP __FILE__ 继承

我正在尝试编写一种可以在使用文件完整路径的扩展类中使用的方法。但是,当我使用__FILE__时,我只会返回定义继承方法的类的路径。这是一个示例:foo.phpclassfoo{publicfunctiongetFullFile(){return__FILE__;}}酒吧.phpclassbarextendsfoo{publicfunctiongetFile(){return__FILE__;}}例子.php$foo=newfoo();echo$foo->getFullFile().PHP_EOL;$bar=newbar();echo$bar->getFullFile().PHP_EOL

php - 在 PHP 5.2 中,如何在子类中创建一个常量以用于父类中的方法?

编辑:*注意:很遗憾,我目前使用的是PHP5.2。我找不到像样的廉价主机提供5.3...在PHP中,self指的是定义被调用方法的类。这意味着如果您不重写子类中的方法,关键字self将引用父类,即使是从子类中调用也是如此。例如,这段代码:\n";}}classChildClassextendsParentClass{constNAME="ChildClass";publicfunction__construct(){echoself::NAME."\n";}}$test=newChildClass();$test->showName();?>将创建此输出:ChildClassParen

php - 有没有办法让 PHP 子类继承属性(静态和实例)?

如果我声明一个基类如下:abstractclassParent{protectedstatic$message="UNTOUCHED";publicstaticfunctionyeah(){static::$message="YEAH";}publicstaticfunctionnope(){static::$message="NOPE";}publicstaticfunctionlateStaticDebug(){return(static::$message);}}然后扩展它:classChildextendsParent{}然后这样做:Parent::yeah();Parent:

php - 为什么分配对静态类变量的引用会破坏此类变量的继承?

我不明白为什么在下面的代码中,$my_foo和$my_bar被子类正确继承,但是如果我通过分配对$my_var的引用来更改$my_foo,子类仍然看到原始值..编辑:这是一个类似的问题:doextendedclassesinheritstaticvarvalues(PHP)?但我的更侧重于继承和引用。EDIT2:请注意,这个问题的重点不是关于后期静态绑定(bind),而是因为$my_foo和$my_bar是继承的,所以在Foo中更改它们不会影响在Bar中访问它们的原因。而这只发生在引用中。事实上,如果我们改变:publicstaticfunctionbreak_inheritance(

php - 使用 Doctrine 的 Symfony2 抽象类多重继承

我得到了以下UML方案:基本上,这是分类系统的开始,其中一些是可嵌套的,而另一些则不是。我开始尝试制作2层抽象类(Taxonomy和OfferCategory),因为它们都不能用作最终实体。我使用了MappedSuperClass,但出现以下错误:[Doctrine\ORM\ORMException]Columnname`id`referencedforrelationfromLCH\CatalogBundle\Entity\HomeOfferCategorytowardsLCH\CatalogBundle\Entity\OfferCategorydoesnotexist.我的主键字段

php - 如何在php中实现鸡鸡蛋鸟类接口(interface)问题

我得到了下面的代码挑战。它是关于接口、类继承等的,不知道怎么做。这是我得到的//interfacebirdinterfaceBird{//layeggpublicfunctionlayEgg();}//chickencanlayeggclassChickenimplementsBird{publicfunctionlayEgg(){returnnewEgg();}}//classeggclassEgg{public$count=0;//egg,birdtypepublicfunction__construct($birdType){}//hatch,bornchickpublicfun

PHP 继承 : child class overriding parent variable/property for use in constructor

我有一个(抽象的)父类应该在构造期间提供功能。子类可以覆盖构造函数中使用的属性:classParentextendsMiddlewareTest{//abstractchannelpropertiesprotected$title=NULL;protected$type=NULL;protected$resolution=NULL;function__construct(){parent::__construct();$this->uuid=$this->createChannel($this->title,$this->type,$this->resolution);}}classC

php - OO : Inheritance vs Service

关闭。这个问题是opinion-based.它目前不接受答案。想改善这个问题吗?更新问题,以便可以通过editingthispost用事实和引文回答问题.6年前关闭。ImprovethisquestionClassA{publicfunction__construct(Foo$foo,Bar$bar,MyCustomType1$mct){//...}//...publicfunctiongetFooBarFunction(){$this->foo->aMethod();$this->bar->anotherMethod();//someotherexecutionhere}}Class