Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126694 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id F329C1A00BC for ; Mon, 10 Mar 2025 16:15:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1741623175; bh=iJ9Sl9DxWdIFYErOC7UoEbEe7J78TIwTkTZDid1DDmo=; h=From:Date:Subject:To:From; b=ll1075WQtAOZIzf0zFh+hLcnK2Lhj9h3J8buwQyA59kd2gHjVqoMuIZdi628INxZM 5a97YR4UNwi3Md6hwgXAxDH0c78NN6AL7oJlozwpr53dAHyvR4rGqNW4utfwGrpMKS 1Yie+pI2A2qqVqqpx0eIMziJe05pCcSmA+jDSG4Pqmf01zfwlCTTxKVuss6EKJ4oO3 Jt9VElpQGymV2qJp6+yENyQzdCXe8pxc4rDDKHsJ2hRO/cpcCW8X8f5jdd/u6HrLj7 SFq9tpEWXA0ATh7HgeoCqDM7hGCaBCYSq+z1ADcNciwE4qjzl5psfAzfp20EYsL5+g UMR9PDRhYTynw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B844B180034 for ; Mon, 10 Mar 2025 16:12:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 10 Mar 2025 16:12:54 +0000 (UTC) Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-2ff694d2d4dso6288937a91.0 for ; Mon, 10 Mar 2025 09:15:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741623327; x=1742228127; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=iJ9Sl9DxWdIFYErOC7UoEbEe7J78TIwTkTZDid1DDmo=; b=IG0dS3UjP4a+yfYqBKfM1s4sc4K/h8XHPcXT2wOh5ZfuEx+KufXmX6wU3oVcY2OQAZ pS0Z6nN68Hs3ohTEww7VmVleDN25efJecZNqkSiGbtk/8ZAQN5LRggoxH+21dbB1Y9fO PRXKoocdoJqHvNkaCRCkmndzQcHgPfGmUIhAWYZJVFnM1wKPoMTGkHxjHD6Gn+E8/P1x wRwZXDmN0lWcI1ZUfaEB4tBxcjlN6z94vHYKgIM0pd/Z6QbE8URoZhbAs+qb29C2fFwc n5ImgdQFgrF1wC2oIwlVulOIDU+BW2be0q5SJ3A0QVn5s4DEyBz+uONn31l6HMEE9e4h +q/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741623327; x=1742228127; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=iJ9Sl9DxWdIFYErOC7UoEbEe7J78TIwTkTZDid1DDmo=; b=ZwlwEalE4ZCPlZh6Nc15tW4MuGCU+zvIzNgIEKnCuBENDSik98nRTuSvHjFY48dgoT 5hwPrZBvh1vyqXg6A0IiwoQlVzCz4cTn8/wONqXMx9Cs5vUOCf4aNluzloVazJaCwZNi c/yKTZbFbMNF5TOjiLFGT7yuq9GT7GKi80NUenkI9x5a+UQuYWHmCMCH7yqUM9yzFVro m3uzAARlDC0kq5BIZDUrNqhSzpl4tAWV06l3jXsdOMRru+E12m+AIIYXn1c1kJ1hwuUv 9i1SKfo6t5665dvb8Ez71BbpY6r9/7a34WpGn6Tmt8S0HuvlfVPCdAA7wqcpBeTDEwRI kEGg== X-Gm-Message-State: AOJu0Yyr7u2doqtwK83lQjGv8SJ64mAkjf32eaVwwMPKFg+rDSnTkKny osfl0jpzm3KjrntS5m/u+IzgBLaPJ0Go+7Yg49K0Mksr8bXLfJu/kA2zzqT8VIo2f5ll2RZqNYP ETbpyQ8QXyLaaDoUadLXFFiK6YMD5qpDw9l8= X-Gm-Gg: ASbGnctY9wHuOG2OBzJBqCC7NxsuxF8DwnSLGNOEtMnGprLhrssIT9PTqtpINM9YdDg Wx62PaxPAdeLvURad5jl6NOWeTVbXWo6PZxQ0++kjqW5uPtYaNSxFH6T9SIc43pjTX/cOjs1v6+ MB48Rh21hBbxWsrd30Lo/xU9NT+XGeoOEvxHa1IH24SGk= X-Google-Smtp-Source: AGHT+IGJb6Oc2AMzZ+6G86hEQxMQRM58u1u6KM2sBdqcnbqioztHRr4YngMdqvGY+P8s2hE3Xc4S6sOFyn/4xkYP2QU= X-Received: by 2002:a17:90b:3c8d:b0:2ee:cded:9ac7 with SMTP id 98e67ed59e1d1-2ff7ce93e31mr22572076a91.20.1741623327372; Mon, 10 Mar 2025 09:15:27 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Mon, 10 Mar 2025 13:15:16 -0300 X-Gm-Features: AQ5f1JonW7oBw9Mipq7DUwP_VpXdtbEYdrWVEceBAtDpnky8UY6DafgF4ZAGCpo Message-ID: Subject: [PHP-DEV] Manual unset and GC To: PHP internals Content-Type: text/plain; charset="UTF-8" From: carlosv775@gmail.com (Vinicius Dias) Hello, folks. If this is not the correct place to ask, I apologize. > TL;DR: Does setting a variable to `null` (or even `unset`ing it) have **any** effect if that's the last instruction of a function? The full version of the question with context: I have a question about the inner workings of PHP that was raised by some people at work. In the code we write at the company I work for, there's a guideline to always assign `null` to the variables of type `PdoStatement`. Something like the following: ``` $stm = null; ``` I understand this removes the reference to the object, calls the destructor and frees the memory, but the point is: the guideline mandates that we do that even if it is the last instruction of a function, for example: ``` function example(): void { // run your SQL queries $stm = null; } ``` I understand that this last line is not needed and removing it would have literally no effect on the code execution since `$stm` will go out of scope and the same things (remove reference, call destructor and free memory) will happen exactly the same. While discussing this with a few colleagues it was pointed out that **maybe** PHP will execute the GC immediately when we do `$stm = null` but not when the variable goes out of scope, making the explicit null as some sort of optimization. I didn't find any resources on the documentation that could point to which assumption is correct, so I post the question here: Does setting a variable to `null` (or even `unset`ing it) have **any** effect if that's the last instruction of a function? Thank you