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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据