How to implement initialize

Source: Internet
Author: User
About how initialize implements the assumption that class a, B, base, and a and B inherit base,
A and B allow a method initialize ();

If new N times:
$ Obj = new ();
$ Obj = new ();
$ Obj = new ();
$ Obj = new B ();
$ Obj = new B ();
$ Obj = new B ();

Execute only one a-> initialize (); and one B-> initialize ();

Initialize mainly replaces the _ construct method in the framework.
Phalcon implements this function, but phalcon is a framework written in C. can PHP achieve this effect?


Reply to discussion (solution)

class bass {  function __construct() {    $this->_initialize();  }  function _initialize() {    static $v;    @$v++;    if($v == 1 && method_exists($this, 'initialize')) $this->initialize();  }}class a extends bass {  function initialize() {    echo __CLASS__;  }}class b extends bass {  function initialize() {    echo __CLASS__;  }}$obj = new a();$obj = new a();$obj = new a();$obj = new b();$obj = new b();$obj = new b();
AB

class bass {  function __construct() {    $this->_initialize();  }  function _initialize() {    static $v;    @$v++;    if($v == 1 && method_exists($this, 'initialize')) $this->initialize();  }}class a extends bass {  function initialize() {    echo __CLASS__;  }}class b extends bass {  function initialize() {    echo __CLASS__;  }}$obj = new a();$obj = new a();$obj = new a();$obj = new b();$obj = new b();$obj = new b();
AB


@ $ V ++;
Why does "@" match this sentence?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.