Cara menggunakan php hasproperty

Fungsi property_exists()_ dapat digunakan untuk memeriksa apakah properti ada di kelas atau objek. Sintaksnya adalah sebagai berikut

property_exists(
    object|string $object_or_class,
    string $property
): bool

Anda harus melewati dua hal

  1. Objek atau kelas sebagai argumen pertama
  2. Properti dalam string sebagai argumen kedua

Berikut adalah contoh pemanggilan fungsi

<?php
class Car {
    public $type;
    private $color;
}

// 👇 check class property
var_dump(property_exists("Car", "type")); // true

// 👇 check object property
var_dump(property_exists(new Car(), "color")); // true

// 👇 instantiate the object first
$car = new Car();
var_dump(property_exists($car, "wheels")); // false
?>
_

Saat memeriksa properti kelas, Anda meneruskan nama kelas sebagai string. Saat memeriksa objek, Anda harus meneruskan instance objek

Dengan meneruskan nama properti sebagai string seperti yang ditunjukkan di atas, fungsi property_exists() akan memeriksa apakah properti tersebut ada dalam nama atau objek kelas yang diberikan

Metode hasOwnProperty() dalam JavaScript digunakan untuk memeriksa apakah objek memiliki properti yang ditentukan sebagai propertinya sendiri. Ini berguna untuk memeriksa apakah objek telah mewarisi properti daripada menjadi miliknya sendiri

Sintaksis

object.hasOwnProperty( prop )

Parameter. Metode ini menerima satu parameter

  • menopang. Itu menyimpan nama dalam bentuk String atau Simbol dari properti untuk diuji

Nilai Pengembalian. Ini mengembalikan nilai boolean yang menunjukkan apakah objek memiliki properti yang diberikan sebagai propertinya sendiri

Contoh di bawah ini mengilustrasikan metode JavaScript hasOwnProperty()

Contoh 1. Contoh ini memeriksa properti dari suatu objek

JavaScript




<p>

    Click on the button to checkif

    the properties are of the object.

</p>

<p>Outputfor <p>0

    <p>2<p>3<p>4

</p>

<p>Outputfor <p>8

    <p>2    1<p>4

</p>

    4    5    6

        8

    _9

Click on the button to check0Click on the button to check1    6

    Click on the button to check4 Click on the button to check5

Click on the button to check6Click on the button to check7

Click on the button to check6Click on the button to check9

Click on the button to check6if1

if_2

Click on the button to check6if4

Click on the button to check6if6if7if8

if_2

Click on the button to check6    1

Click on the button to check6    3    4if8

if_2

Click on the button to check6    8    9the properties are of the object.0

the properties are of the object.1the properties are of the object.2

the properties are of the object._3

Click on the button to check6    8the properties are of the object.6the properties are of the object.0

the properties are of the object.1the properties are of the object.9

    </p>1

</p>_2

Keluaran

Cara menggunakan php hasproperty

Metode JavaScript hasOwnProperty()

Contoh 2. Contoh ini memeriksa properti objek kelas

html




</p>3</p>4    6

    </p>7

    </p>_9

________12______0</p>4    6

________11property_exists()_11_______4<p>Output5

    </p>3<p>Output8 <p>Output9for0<p>3for2<p>Output8    6

________12______0</p>4    6

________11property_exists()_11_______4<p>00

    </p>3<p>Output8 <p>Output9for0    1for2<p>Output8    6

________12______0</p>4    6

</p>3<p>14 <p>15for0    5    6

        8

________12______0<p>14    6

<p>_24

</p>3<p>26 <p>27for0Click on the button to check1    6

    <p>32

the properties are of the object._3

Click on the button to check6<p>35

<p>36<p>37

<p>36<p>39

Click on the button to check6</p>1

if_2

Click on the button to check6<p>44

if_2

Click on the button to check6if4

Click on the button to check6<p>49

if_2

Click on the button to check6    1

Click on the button to check6<p>54

if_2

Click on the button to check6<p>57

the properties are of the object.1the properties are of the object.2

the properties are of the object._3

Click on the button to check6<p>62

the properties are of the object.1the properties are of the object.9

    </p>1

________12______0<p>26    6

Keluaran

Cara menggunakan php hasproperty

Metode JavaScript hasOwnProperty()

Kami memiliki daftar lengkap metode Objek, dan properti untuk memeriksanya, silakan baca artikel Referensi Lengkap Objek JavaScript ini