Jump to content

Recommended Posts

Проверка по типу в PHP возможно делать так:

  1. Просто if' ами:
    if($object instanceOf TestClass1) {
    	echo "TestClass1";
    } elseif($object instanceOf TestClass2) {
    	echo "TestClass2";
    } elseif($object instanceOf TestClass3) {
    	echo "TestClass3";
    } elseif($object instanceOf TestClass4) {
    	echo "TestClass4";
    }

     

  2. В switch по имени класса:
    $class = get_class($object);
    switch($class) {
        case 'TestClass1':
            echo "TestClass1";
            break;
        case 'TestClass2':
            echo "TestClass2";
            break;
        case 'TestClass3':
            echo "TestClass3";
            break;
        case 'TestClass4':
            echo "TestClass4";
            break;
    }

     

  3. И внутри switch'a проверка:
    switch(true) {
        case $object instanceof TestClass1:
            echo "TestClass1";
            break;
        case $object instanceof TestClass2:
            echo "TestClass2";
            break;
        case $object instanceof TestClass3:
            echo "TestClass3";
            break;
        case $object instanceof TestClass4:
            echo "TestClass4";
            break;
    }

     

 

 

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...