{
    "slug": "php_opaque_objects",
    "term": "Opaque Objects & GdImage/CURLHandle",
    "category": "php",
    "difficulty": "intermediate",
    "short": "PHP 8.0 replaced resource types (gd, curl, xml) with opaque objects — GdImage, CurlHandle, XMLParser — improving type safety and OOP integration.",
    "long": "Before PHP 8.0, many extensions returned resource types (is_resource() check, no type hints). PHP 8.0+ returns typed objects: imagecreatetruecolor() returns GdImage, curl_init() returns CurlHandle, xml_parser_create() returns XMLParser. These are opaque — you can't inspect their internals — but they're proper objects with type hints and are accepted by instanceof. Old code using is_resource() checks breaks. Type hints now possible: function process(GdImage $image). These objects are still reference-counted and freed when no longer referenced, similar to resources.",
    "aliases": [],
    "tags": [
        "php",
        "php8",
        "resources",
        "gd",
        "curl"
    ],
    "misconception": "Opaque objects like GdImage behave like regular PHP objects — they're opaque wrappers with no accessible properties, just type safety.",
    "why_it_matters": "Typed opaque objects enable type hints for extension functions, eliminating the need for is_resource() checks and enabling IDE/static analysis support.",
    "common_mistakes": [
        "Using is_resource() on GdImage/CurlHandle in PHP 8.0+ — always returns false.",
        "Not type-hinting function parameters accepting GdImage/CurlHandle.",
        "Trying to clone or serialize opaque objects — not supported."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_deprecation_notices",
        "php_eol_schedule",
        "php_extensions"
    ],
    "prerequisites": [
        "php_extensions"
    ],
    "refs": [
        "https://www.php.net/manual/en/migration80.incompatible.php"
    ],
    "bad_code": "$image = imagecreatetruecolor(100, 100);\nif (!is_resource($image)) { // Always false in PHP 8!\n    throw new Exception('Failed to create image');\n}",
    "good_code": "$image = imagecreatetruecolor(100, 100);\nif (!($image instanceof \\GdImage)) {\n    throw new \\RuntimeException('Failed to create image');\n}\n\nfunction applyFilter(\\GdImage $img, int $filter): void {\n    imagefilter($img, $filter);\n}",
    "quick_fix": "Replace is_resource() checks with instanceof GdImage / CurlHandle / XMLParser. Add type hints to functions accepting these objects.",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_opaque_objects",
        "html_url": "https://codeclaritylab.com/glossary/php_opaque_objects",
        "json_url": "https://codeclaritylab.com/glossary/php_opaque_objects.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Opaque Objects & GdImage/CURLHandle](https://codeclaritylab.com/glossary/php_opaque_objects) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_opaque_objects"
            }
        }
    }
}