第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何區(qū)分 PHP 屬性是否未定義或設(shè)置為 NULL

如何區(qū)分 PHP 屬性是否未定義或設(shè)置為 NULL

PHP
qq_笑_17 2023-09-08 14:19:46
所以我面臨這個(gè)問(wèn)題。我有一個(gè)類(lèi)代表數(shù)據(jù)庫(kù)中的一條記錄(本例中為 User)。該類(lèi)具有與數(shù)據(jù)庫(kù)表的列一樣多的屬性。為簡(jiǎn)單起見(jiàn),我的示例中只有三個(gè):$id- 用戶(hù)的ID(對(duì)于注冊(cè)用戶(hù)必須設(shè)置為正整數(shù),對(duì)于尚未保存在數(shù)據(jù)庫(kù)中的用戶(hù)對(duì)象可能設(shè)置為0)$name- 用戶(hù)名(必須為每個(gè)用戶(hù)設(shè)置,但在從數(shù)據(jù)庫(kù)加載之前可能未定義)$email- 用戶(hù)的電子郵件地址(如果用戶(hù)未提交電子郵件地址,則可能為 NULL)我的(簡(jiǎn)化的)課程如下所示:<?phpclass User{  private $id;  private $name;  private $email;    public function __construct(int $id = 0)  {      if (!empty($id)){ $this->id = $id; }      //If $id === 0, it means that the record represented by this instance isn't saved in the database yet and the property will be filled after calling the save() method  }    public function initialize(string $name = '', $email = '')  {      //If any of the parameters isn't specified, prevent overwriting curent values      if ($name === ''){ $name = $this->name; }      if ($email === ''){ $email = $this->email; }            $this->name = $name;      $this->email = $email;  }    public function load()  {      if (!empty($this->id))      {          //Load name and e-mail from the database and save them into properties      }  }  public function save()  {      if (!empty($this->id))      {          //Update existing user record in the database       }      else      {          //Insert a new record into the table and set $this->id to the ID of the last inserted row      }  }    public function isFullyLoaded()  {      $properties = get_object_vars($this);      foreach ($properties as $property)      {          if (!isset($property)){ return false; }   //TODO - REPLACE isset() WITH SOMETHING ELSE      }      return true;  }    //Getters like getName() and getId() would come here}現(xiàn)在終于解決我的問(wèn)題了。正如您所看到的,可以在不設(shè)置所有屬性的情況下創(chuàng)建此類(lèi)的實(shí)例。getName()如果我想在名稱(chēng)未知的情況下進(jìn)行調(diào)用(未通過(guò)initialize()方法設(shè)置并且未調(diào)用 load() ),那么這是一個(gè)問(wèn)題。為此,我編寫(xiě)了一種方法isFullyLoaded(),該方法檢查所有屬性是否已知,如果不已知,load()則應(yīng)調(diào)用(從調(diào)用的方法中調(diào)用isFullyLoaded())。問(wèn)題的核心是,某些變量可能是空字符串('')、零值(0 )甚至 null (如$email屬性)。所以我想?yún)^(qū)分設(shè)置了任何值(包括 null)的變量和從未分配過(guò)任何值的變量。TL:DR PHP中如何區(qū)分未定義變量和已賦值為NULL的變量?
查看完整描述

3 回答

?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

這是引入自定義Undefined類(lèi)(作為單例)的另一種方法。此外,請(qǐng)確保鍵入您的類(lèi)屬性:


class Undefined

{

    private static Undefined $instance;


    protected function __constructor()

    {

    }


    protected function __clone()

    {

    }


    public function __wakeup()

    {

        throw new Exception("Not allowed for a singleton.");

    }


    static function getInstance(): Undefined

    {

        return self::$instance ?? (self::$instance = new static());

    }

}


class Person

{

    private int $age;


    public function getAge(): int|Undefined

    {

        return $this->age ?? Undefined::getInstance();

    }

}


$person = new Person();


if ($person->getAge() instanceof Undefined) {

    // do something

}

但使用單例模式有一個(gè)缺點(diǎn),因?yàn)閼?yīng)用程序中所有未定義的對(duì)象將嚴(yán)格彼此相等。否則,每個(gè)返回未定義值的get 操作都會(huì)產(chǎn)生副作用,即另一塊分配的 RAM。


查看完整回答
反對(duì) 回復(fù) 2023-09-08
?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

PHP 不像 javascript 那樣具有未定義的值。但它不是嚴(yán)格類(lèi)型的,所以如果您沒(méi)有找到更好的解決方案,這里有一個(gè)自定義類(lèi)型 UNDEFINED


<?php

class UNDEFINED { }


class Test {

var $a;


    function __construct( $a='' ) {

            $this->a = new UNDEFINED();

            if( $a !== '' ) {

                    $this->a = $a;

            }

    }



    function isDefined() {

            $result =true;

            if(gettype($this->a) === 'object'){

             if(get_class($this->a) === 'UNDEFINED') {

               $result=false;

             }

            }


            echo gettype($this->a) . get_class($this->a);

            return $result;

    }


}


$test= new Test();


$test->isDefined();

這是一個(gè)可能更好的版本,它使用 instanceof 而不是 get_call 和 getType


<?php

class UNDEFINED { }


class Test {

  var $id;

  var $a;

  var $b;


  function __construct( $id) {

    $this->id = $id;

    $this->a = new UNDEFINED();

    $this->b = new UNDEFINED();

  }


  function init( $a = '' , $b = '') {

    $this->a = $this->setValue($a,$this->a);

    $this->b = $this->setValue($b,$this->b);

  }


  function setValue($a,$default) {

    return $a === '' ? $default : $a;

  }


  function isUndefined($a) {

    return $a instanceof UNDEFINED;

  }

 

  public function isFullyLoaded()

  {

    $result = true;

    $properties = get_object_vars($this);

    print_r($properties);

    foreach ($properties as $property){

      $result = $result && !$this->isUndefined($property);

      if ( !$result) break;

    }

    return $result;

  }


  function printStatus() {

    if($this->isFullyLoaded() ) {

      echo 'Loaded!';

    } else {

      echo 'Not loaded';

    }

  }

}


$test= new Test(1); 

$test->printStatus();

$test->init('hello');

$test->printStatus();

$test->init('', null);

$test->printStatus();


查看完整回答
反對(duì) 回復(fù) 2023-09-08
?
明月笑刀無(wú)情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

用途property_exists():


<?php


error_reporting(E_ALL);


// oop:


class A {

    public $null_var = null;

}


$a = new A;


if(property_exists($a, 'null_var')) {

    echo "null_var property exists\n";

}


if(property_exists($a, 'unset_var')) {

    echo "unset_var property exists\n";

}


// procedural:


$null_var = null;


if(array_key_exists('null_var', $GLOBALS)) {

    echo "null_var variable exists\n";

}


if(array_key_exists('unset_var', $GLOBALS)) {

    echo "unset_var variable exists\n";

}


// output:

// null_var property exists

// null_var variable exists


查看完整回答
反對(duì) 回復(fù) 2023-09-08
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)