douaipi3965 2013-03-29 01:36
浏览 62
已采纳

__Construct在OOP类中? [关闭]

I am newish to the OOP style of PHP5, I have noticed __construct and __deconstruct within example classes and production classes.

I have read over the manual of this:

http://php.net/manual/en/language.oop5.decon.php

and looked over a range of questions/answers on StackOverflow. I'm still having trouble to understand what is the actual meaning of its exisitance?

class foo {
   function __construct()
   {
     // do something 
   }

   public function Example ()
   {
    echo "Example Functions";
   }

   function __destruct()
   {
     // do something
   }
}

The same class can function the same with no hits as:

class foo {
       public function Example ()
       {
        echo "Example Functions";
       }
    }

But the manual states with the above example, that my first function will take over the role as the __construct

Why is this a priority within PHP5 OOP Classes?

  • 写回答

3条回答 默认 最新

  • dongxiaoguang9108 2013-03-29 01:41
    关注
    class Foo {
        public function __construct() {
            print("This is called when a new object is created");
            // Good to use when you need to set initial values,
            // (possibly) create a connection to a database or such.
        }
    
        public function __destruct() {
            print("This is called when the class is removed from memory.");
            // Should be used to clean up after yourself, close connections and such.
        }
    }
    
    $foo = new Foo();
    

    Addition,

    class Person {
    
        private $name; // Instance variable
        private $status; // Instance variable
    
        // This will be called when a new instance of this class i created (constructed)
        public function __construct($name, $age) {
            $this->name = ucfirst($name); // Change input to first letter uppercase.
    
            // Let the user of our class input something he is familiar with,
            // then let the constructor take care of that data in order to fit
            // our specific needs.
            if ($age < 20) {
                $this->status = 'Young';
            } else {
                $this->status = 'Old';
            }
        }
    
        public function printName() {
            print($this->name);
        }
    
        public function printStatus() {
            print($this->status);
        }
    }
    
    $Alice = new Person('alice', 27);
    $Alice->printName();
    $Alice->printStatus();
    

    /Addition

    If you run the above code and read the comments you should be able to understand when and how constructors and destructors should be used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题