dtvam48220 2011-10-03 13:17
浏览 24
已采纳

PHP中的动态扩展

I am new on SO, so I hope I make myself clear in the following question.

I have been struggling myself recently trying to write a framework for a project. The problem is that I am new to PHP OOP and I am not sure how to extend an object that can be changed in the future and still keep the properties available in the extended class.

More precisely:

class main {
function __construct() {
$this->a='some a text';
$this->b='some b text';
}
}

class submain extends main() {
function __construct() {
parent::__construct();
$this->c='some c text';
}
}

$main=new main();
$submain=new submain();

So now the $submain variable contains 3 properties a,b and c.

What if now I do $main->d='some d text';

How do I make so that the d property appears in the $submain variable which extended the main class?

I suppose I should be using cloning?!

  • 写回答

2条回答 默认 最新

  • doushu5805 2011-10-03 13:43
    关注

    Starting with the Edited Answer

    I now understand the question to be: How can you set a property on one object and have it set in all classes that derive from it (at runtime).

    Sorry, I think my best answer can only be a philosophical one.

    Certain languages favour certain approaches to solving problems. (Actually in javascript this would be really easy). Writing a class in PHP defines the object that gets created when the class is instantiated. Once an object is created methods of the class and inherited methods can be called to perform actions that the object is designed to do.

    Many objects can be created which each encapsulate a part of your system. Making each object do a specific job and keeping its coupling to other parts of the system low enables large systems to be built in specific testable parts.

    Each object created through a class is specific (possibly sharing static members - although static may be considered bad design). You would have to force PHP to dynamically share settings between objects of the same class. Now that I have hopefully dissuaded you from doing so - here is how you could do it. Notice that it looks ugly and will cause you infinite problems in the future if your program gets to any size. Please don't use what I am posting here. I didn't test this code or its syntax, I never write static stuff so there are probably mistakes, but I think this idea would work (and be horrible).

    class main
    {
       // Yes its defined statically but with an array you can do anything.
       public static $settings;
    
       public function __construct() {}
    
       public defineVar($varName, $varValue)
       {
           self::$settings[$varName] = $varValue;
       }
    
       public static function get($varName)
       {
          return self::$settings[$varName] = $varValue;
       }
    }
    
    // Submain as before.
    
    $t = new submain();
    
    main::defineVar('d', 'some d text');
    
    $t->get('d');
    

    This will create a dynamically definable value that you can store in your main and all derived classes. Their values will be the same throughout all objects. If you want a value that can be different for the same variable through the classes you would basically be setting up a registry system (I think there might be a design pattern for that).

    Original Answer Below

    I would write this with the access types of public, protected and private which are important in OOP.

    All objects you create of class main will contain the properties as set by their constructor. Same goes for submain so you would add $d just as an a, b or c entry. I'll add it to the main, the submain constructor already calls its parent constructor so will construct objects with properties a, b, c and d defined.

    class main
    {
       public $a;
       protected $b;
       public $d;
    
       public function __construct()
       {
          $this->a='some a text';
          $this->b='some b text';
          $this->d='some d text';
       }
    }
    
    class submain extends main()
    {
       public $c;
    
       public function __construct()
       {
          parent::__construct();
          $this->c='some c text';
       }
    
       public function getB()
       {
          return $this->b;
       }
    }
    
    $main=new main();
    echo $main->a; // Publicly accessible.
    // Can't do $main->b() it is protected.
    echo $main->d;
    
    $submain=new submain();
    echo $submain->a; // Still publicly accessible.
    echo $submain->getB(); // protected variable exposed by public function.
    echo $submain->d;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度