简体   繁体   中英

Php multiple class extend one

is possible to extend php classes (multiple class extend one ).

-----init.php------

include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/functions.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/template.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/debug.php';

-----Functions.php------

    class Functions{
         // content here
      }

-----Template.php------

    class Template extends Functions{
          public $sTitle = "";

          public function setTitle($title){$this->sTitle = $title;}
        }

-----debug.php------

class debug extends Functions{
        // content here
            }

Functions:Template->setTitle('my title');

if you know a better whay to do this, please share with me. (this classes are in different files that are included by "class Functions" file)

Of course you can. Actually, you can test yourself with a minimal compiling example :

<?php

class SuperClass {

  public function __construct(){ echo 'I am the common superclass.  '; }
}

class LittleClass extends SuperClass{}
class LittleLittleClass extends SuperClass{}

new LittleClass();
new LittleLittleClass();

Besides that, calling a class Functions smells a lot like very bad design. If you want to include general purpose functions in your codebase, consider collecting them somewhere else than in the inheritance chain.

You can include function setTitle() that do nothing to super class (Functions in your case). Then in Template you can override its functionality. In this case it is safe to run this function on 'debug' class. Ie

----init.php------

include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/functions.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/template.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/lib/php/debug.php';

-----Functions.php------

    class Functions{
         // content here
    public function setTitle($title){}
      }

-----Template.php------

    class Template extends Functions{
          public $sTitle = "";

          public function setTitle($title){$this->sTitle = $title;}
        }

-----debug.php------

class debug extends Functions{
        // content here
            }

Functions:Template->setTitle('my title');

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