简体   繁体   English

解释简单的PHP代码

[英]Interpreting simple PHP code

To illustrate the concept of Destructor that a database is updated if values changes before the object is destroyed following code block is given in the book : 为了说明Destructor的概念,在本书中给出以下代码块后,如果值在对象被销毁之前发生更改,则数据库将更新:

<?php
  class use {
  private $_properties;
  private $_changedProperties //Keeps a list of the properties that were altered
  private $_hDB;

  //_construct and __get omitted for brevity

  function __set($propertyName, $value) {
     if(!array_key_exists($propertyName, $this->_properties))
     throw new Exception('Invalid property value!');

   if(method_exists($this, 'set'. $propertyName)) {
   return call_user_func(
                       array($this, 'set', $propertyName), $value);
    }
    else {
    //If the value of the property really has changed
    //and it's not already in the changedProperties array,
    //add it.

    if($this->_properties[$propertyName] !=$value && !in_array($propertyName,        $this->_changedProperties)) {
      $this->_changedProperties[] = $propertyName;
     }

Rest of the code was unnecessary code and has been omitted. 其余代码是不必要的代码,已被省略。 Please explain the code from the point: 请从以下角度解释代码:

        if(method_exists($this, 'set'. $propertyName)) {
   return call_user_func(
                       array($this, 'set', $propertyName), $value);
    }
    else {
    //If the value of the property really has changed
    //and it's not already in the changedProperties array,
    //add it.

    if($this->_properties[$propertyName] !=$value && !in_array($propertyName, $this->_changedProperties)) {
      $this->_changedProperties[] = $propertyName;
     }

Why am i asking this is that, I want to verify my interpretation/understanding of the code. 我为什么要问这个,我想验证我对代码的解释/理解。

Your commented notes seem correct... but this has little to do with the actual destructor, though i assume the destructor checks the changedProperties member and writes them if there are any before destruction. 您评论的注释似乎是正确的……但这与实际的析构函数无关,尽管我认为析构函数会检查changedProperties成员并在销毁前将其写入。 But thats not really pertinent to your question so i think youre causing confusion by mentioning it. 但这与您的问题并不完全相关,因此我认为您通过提及它会引起混乱。

Roughly, this code checks to see if there is a setter (a method that sets a value on a property) for the property with name given by the argument $propertyName , and if no such function exists it adds that property to an field that contains an array called _changedProperties . 大致而言,此代码检查以参数$propertyName给出的名称是否具有该属性的setter(一种在属性上设置值的方法),如果不存在此类函数,它将将该属性添加到包含以下内容的字段中一个名为_changedProperties的数组。

More precisely: suppose $propertyName contains a string "Foo" 更准确地说:假设$propertyName包含字符串"Foo"

if(method_exists($this, 'set'. $propertyName)) {

If this object has a method (sometimes called a function) with the name setFoo 如果此对象具有名称为setFoo的方法(有时称为函数)

    return call_user_func(array($this, 'set', $propertyName), $value);

Invoke the method setFoo with first argument $value and return its result; 使用第一个参数$value调用方法setFoo并返回其结果; equivalent to calling return $this->setFoo($value); 等效于调用return $this->setFoo($value); , but the text Foo is parameterized by $propertyName . ,但文本Foo$propertyName参数化。

}else{

Basically, this object has no method called setFoo . 基本上,此对象没有名为setFoo方法。

    if($this->_properties[$propertyName] != $value 
       && !in_array($propertyName, $this->_changedProperties)) {

If the value of this property ( Foo ) has a stored value different to the one I know now and it does not appear in the _changedProperties array 如果此属性( Foo )的值具有与我现在知道的值不同的存储值,并且它不出现在_changedProperties数组中

        $this->_changedProperties[] = $propertyName;

Add the name of this property to the list of changed properties. 将此属性的名称添加到已更改的属性的列表。 (here, add Foo to the _changedProperties array. (在这里,将Foo添加到_changedProperties数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM