Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126707 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 62F951A00BC for ; Tue, 11 Mar 2025 00:06:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1741651460; bh=P6dWHxfI80WbMqieuqsQIT0DtJrzTxq42+5dbfw6h8k=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=LupMoRJDChvEhrCsYAhI/cqva7hCoeZx9qrZkglrDDxppUlB6/YOc6suOhsCrgORd mx0+fi10IYcgkVKFpeignMxr52bKPj+S9za5/gMyygS5mTjIUJy/fVXGm1gpvpe6wa ZsbAU48ilDAab5eUMghFxAxBh0C7GCTxJlVc+HxH/Y0ilnLKaDMJskk41MNg184gua 4S874p/+G1ghk/FwvV+beNbpHYdscw8CQUPD/Z62sGvErF0g+RpH5rANlP9zV6wTJi 8cXReaCs4T2xbuCCHsDDNhedUeISDxjFkNw/v+rj8XV9arJCVli1w3Qpzhh3ZOKYBi 8wxHz+hrMfb3w== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 258AA1801C7 for ; Tue, 11 Mar 2025 00:04:20 +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=-1.7 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_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (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 ; Tue, 11 Mar 2025 00:04:19 +0000 (UTC) Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-30bd21f887aso43931231fa.1 for ; Mon, 10 Mar 2025 17:06:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741651612; x=1742256412; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=P6dWHxfI80WbMqieuqsQIT0DtJrzTxq42+5dbfw6h8k=; b=drcmKMY/6Q/J10JN+Uew8nizQGASgZKhwnlzm8T6bOOcAJX59BWJWzGRvkbJuE81tT GBsfnzjApWrntIna7F6AmZ62vqqvENvfJWiS3ZyDdgL3j/4a9rvi8xZ2NYZpNViCUtqE A9lAt81Fv3jXkKaQeR8pKjnaGZ+bulFWI8IQp1uc2wwznt3G4T41x8X50Xk/vYm9wz4X Xm3adG6d8mEpzNedyUqx6/orPo5xb9FfdvoFJRkQ4Syg1OqcMeMkDml+UK8/5hVHaofm acwK1J62r25cdR78mJth6mLSQ5QUra5obKU1luK8nah7mSJyMsKWaDEEjILYFe4IKEuR RsDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741651612; x=1742256412; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=P6dWHxfI80WbMqieuqsQIT0DtJrzTxq42+5dbfw6h8k=; b=temJO+XUZT3fuvOzZH+iG87POOJ3gCG/P2B8aJJ8LfP2+YW88a4r87FJKdDPrm8/Lk /yagmDiFA2Do+fJy2cBp3xAYPDq2TSvKxim1lHdQDC7hBrVZOdeLBbIWK+7gshIAInTF y/bTGY0v5L/1JAES/I+W4k+O7vEzCNiy4+4NlZpTgt8tEoFV3DB1RluzwKFKGIbcMwj9 9unkFMqtS9gw4AL4bmplWBmckmlTiji3HSeCZY0DVPQfU1+BoQIIRs7A6Btl93i0uw44 OEDqUXB4OqowIbDcKX/sT5TMp7vYcdGUroo6Qv8AB8bGTdEwm//xFEOFAA759LoRcCUh FT8g== X-Gm-Message-State: AOJu0Yxbq3NayOZT3Nu9kvCbXtHNjzjKGdBsWFFXCZkgkSyRvwRss3wg 8k9Vmlc9EgUO481SlPq4NB9/gU9KVbdAXKwwUVkOWM2+sAV47wnbGh3P3yD5RuyfxM4/DUlIII5 0KrDnvM1/MS3fK4yKhheHQVV3pyM= X-Gm-Gg: ASbGncv3/9Hwjl9F2i3AmvFA+XP3poXu+aUxAe0Co0LnPxSPJNArxQ8vhvEyk+52e6n 0P7KxzEI8j97Q1VUf43Kf/HyjwDYqatqbAvdTdxW9A7Wra0FbBY6PupGRs03vfrIz+R/DI4Ld0/ DWyengfsQD/omYg+6lQ5ed9DxaWw== X-Google-Smtp-Source: AGHT+IEw4lP3Rg300S/2+XjQ0egWciozRazrW0ibj/7+IpDOni9bZfAIlEDXu1p3uAkrE4uHzTO2XZHh/2u5m7dgmek= X-Received: by 2002:a2e:b8d2:0:b0:308:eb58:6580 with SMTP id 38308e7fff4ca-30bf46387a6mr50098741fa.33.1741651611377; Mon, 10 Mar 2025 17:06:51 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 11 Mar 2025 00:06:39 +0000 X-Gm-Features: AQ5f1Jpema_qDIKVCQZgjrsFBYB7fpKcf2gyAtEgO4JsbNTIVDZvqlbKQuKBQno Message-ID: Subject: Re: [PHP-DEV] Manual unset and GC To: Vinicius Dias Cc: PHP internals Content-Type: text/plain; charset="UTF-8" From: tekiela246@gmail.com (Kamil Tekiela) Assuming that the variable gets created in the scope of the function and that it never leaks outside of the scope (e.g. by reference), then you can consider these things: PHP will destroy the object when there are no more references to it in the running script[1]. So, if the PDO object is limited to the function's scope, unsetting the variable, setting it to something else, or doing absolutely nothing will result in the same outcome. The code line `$stmt = null` doesn't directly affect the PDOStatement that was assigned to this variable previously. It just sets the value of the variable to something else. https://3v4l.org/VP5IJ Therefore, setting the variable to null as the last statement in the function is just adding an extra unnecessary line of code. I assume OPCache will optimize it away since it is dead code, but if not, then that's just one more operation PHP has to do as part of the function. From my testing, it seems the assignment is not optimized away. So it's the opposite of an optimization. The `$stmt = null` is cargo-cult programming[2]. Most likely, the developer that came up with this guideline learned PHP years ago when mysql_* API was in use. After switching to PDO, they realized there is no close() method and, without understanding why, tried to come up with an alternate "solution". Or maybe they've run into an issue with out-of-sync operations on MySQL and saw setting the variable to null as a quick solution. Later, people kept copying this code without knowing why that line was there. Neither the connection nor the statement should be closed. An attempt to do so is a code smell that indicates that your code is poorly designed and needs to be refactored. In your example, the refactoring involves simply removing the dead-code assignment. In other cases, it could mean restricting the objects only to the relevant scope. In my opinion, there isn't any good reason to unset a variable. [1]: https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.destructor [2]: https://en.wikipedia.org/wiki/Cargo_cult_programming