Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:124730 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 2D61F1ADCD4 for ; Fri, 2 Aug 2024 22:49:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1722639060; bh=JAd7ANOI53fdYgLmXBNFmSiplloUOFsHEtv/EE8O3DU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=VEwSkfIZYYaj5FPoOtOklfAAAm57c6fc3z2Y878yPcX/bLiuES+TCN1ZtpbIASqcg s23Qq/uMa0Ke4HsPF6ZfmmBL1wFftklihwngGLnox2hfdidiLztR4I/XbBxGBHSO6Y EHpbi9gQ8bhzz60V0E5SWjy9MAX/LZXatvtbXlFzgDCItYrt2HN5+WbW8DXBn04MVC QkWZWiB0KJYUzTcVkAUfzwM2ZipCHGhuYLNdOdf4s+/BI15++7gpcjS91En9fSYqbh /af/xXJBqT9mT+sS0ReaEWTOMo5WOf9im0US7Uw8Kxw/L0yFbQIEMwFyknbx7C22lO KvYO5j8rZOq8A== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 84F8A1801DD for ; Fri, 2 Aug 2024 22:50:57 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,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-yb1-f181.google.com (mail-yb1-f181.google.com [209.85.219.181]) (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 ; Fri, 2 Aug 2024 22:50:54 +0000 (UTC) Received: by mail-yb1-f181.google.com with SMTP id 3f1490d57ef6-e05ea6a2808so854770276.3 for ; Fri, 02 Aug 2024 15:49:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722638954; x=1723243754; 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=fF0gAgJz2rnEAQgortZiuGBhcgN2ixRLJ4bYPqKM9jY=; b=NSJcxkcfMZ3qCSy1kI/eos08eTSwMXcGIiIt7e1LH+WtrbDE77Xe5szhSakikwn1NH 1BjbqZzjQzVS9G2NuRL0/lhyFLf4fWfT+KrjxL/ycZ/bbBH3QmZIyScW2a0yN+ZJasw2 SNH18WdOouTKU/HBpNF0OduWvLxQcvw4YW4iRr9Jf8nF1fiZOltz3PzYaANULulVAuad aVHrL/NHFV4hERO3A20PBwi5CghS5JJaYEKH6X+NO5JQ76OoXVZAunnBHhZjx0PZahNk uokCRXV4VYl/kQS7arOnSiBvL8+2wQIQkI1agC7c44aHPq1kvxymFgMwmAfLV3z+112x XAKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722638954; x=1723243754; 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=fF0gAgJz2rnEAQgortZiuGBhcgN2ixRLJ4bYPqKM9jY=; b=LJbT9iR7rrdzX76Z0zeA7nsRFToC8SUWGmrsjl9h/+2gTckr+Oi9FwZwblhcFwtnbu BMS22M+FIjLG7gzYp+DJ8uglUN6Y94ebXwHDxwvujHlZq1riz+1m9ZFyx+CrRIxORWNX t/OkDsrz2BPk4m7+PF1Om+rnxmXhRD5LEGWkm2u1dDGMiiuoyu6wMuh+7Y/AEeiorSY9 YS6D9L9nFb7XEcTnM+n+AfJabspO7NiOSBwr/u8m8JgjV7U+0LqN9riMHwZtshxu5Vz9 EiTQH1vYnsAXzFoF4EA0kfkrSaM1rLK386vYc2mopd7svflLB42g+V9/7x0lExSZrc0K q8nw== X-Gm-Message-State: AOJu0YxrrCzj0GOy1LdEdXDpgQOL01nb6yoZDz3cCEKNk4XgwmStMYQw /4P5Kr/1wl1DVlNl5MdCJzGAcqcZUWCrnFW5VjKlPE87FnuBCEtdL0OwKvpyF+Q4y/oomF8WvQe WyNxV+2eTqc4mtFDeBhT3sbbEhRA2BKlXPfM= X-Google-Smtp-Source: AGHT+IEPuQC2DXW6aT7TZ2Gr/G1UJMYpnUKnSEtJPpfdx8zn4o1LD2bTWVcporGM5otB4r+5nYDScx3qaM0gmVqAOBE= X-Received: by 2002:a25:8503:0:b0:e0b:3a7d:928d with SMTP id 3f1490d57ef6-e0bde2a98aemr2763257276.1.1722638954241; Fri, 02 Aug 2024 15:49:14 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <48c22449-6cdf-449f-917f-365506e3f2d0@scriptfusion.com> In-Reply-To: Date: Fri, 2 Aug 2024 19:48:38 -0300 Message-ID: Subject: Re: [PHP-DEV] [Concept] Flip relative function lookup order (global, then local) To: Nick Lockheart Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000f1a99b061ebb22c1" From: deleugyn@gmail.com (Deleu) --000000000000f1a99b061ebb22c1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Aug 2, 2024 at 7:03=E2=80=AFPM Nick Lockheart wrote: > I was thinking about a similar problem this week. > > If class A relies on class B, but you want to swap out > class B with a stub to test class A in isolation, > is there a way to make every call to class B, > from class A, actually call a different class > during the test, without modifying class A's code? > > > Minimal code for discussion purposes: > > > // conf class in global namespace > abstract class CONF { > const DATABASE_HOST_NAME =3D 'db.example.com'; > const DATABASE_NAME =3D 'production'; > const DATABASE_USER_NAME =3D 'prod_user'; > const DATABASE_PASSWORD =3D '123'; > } > > > // conf class in test namespace: > namespace test; > abstract class CONF { > const DATABASE_HOST_NAME =3D 'db.sandbox.com'; > const DATABASE_NAME =3D 'test'; > const DATABASE_USER_NAME =3D 'test_user'; > const DATABASE_PASSWORD =3D 'abc'; > } > > > // SQL class in global namespace > class SQL { > > private function Init(){ > self::$oPDO =3D new PDO( > 'mysql:host=3D'.CONF::DATABASE_HOST_NAME. > ';dbname=3D'.CONF::DATABASE_NAME.';charset=3Dutf8mb4', > CONF::DATABASE_USER_NAME, > CONF::DATABASE_PASSWORD, > [] > ); > } > } > > // Testing class in test namespace: > namespace test; > class SQLTester { > > // How do I make the SQL class see \test\CONF instead of > // \CONF, when SQL calls for CONF in this test scope, > /// without changing anything inside of the SQL class? > } > > > I think some kind of sandboxing tools would be useful for > build/test/deployment. > You could hack this out using the autoloader, but it's something that the PHP community frowns upon, imo. A much prevalent practice in the PHP ecosystem is a Dependency Injection container. A somewhat similar concept exists in the Javascript ecosystem with hoisting import statements and mocking modules, but if you don't understand the system and are unaware that order of import execution will matter on whether the mock succeeds or not plays a huge role in making it a cumbersome and awkward system. Regardless, it's not possible for functions as users don't control function autoloader. --=20 Marco Deleu --000000000000f1a99b061ebb22c1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Fri, Aug 2, 2024 at 7:03=E2=80=AFP= M Nick Lockheart <lists@ageofdre= am.com> wrote:
db.example.com';
=C2=A0 =C2=A0const DATABASE_NAME =3D 'production';
=C2=A0 =C2=A0const DATABASE_USER_NAME =3D 'prod_user';
=C2=A0 =C2=A0const DATABASE_PASSWORD =3D '123';
}


// conf class in test namespace:
namespace test;
abstract class CONF {
=C2=A0 =C2=A0const DATABASE_HOST_NAME =3D 'db.sandbox.com';
=C2=A0 =C2=A0const DATABASE_NAME =3D 'test';
=C2=A0 =C2=A0const DATABASE_USER_NAME =3D 'test_user';
=C2=A0 =C2=A0const DATABASE_PASSWORD =3D 'abc';
}


// SQL class in global namespace
class SQL {

=C2=A0 =C2=A0private function Init(){
=C2=A0 =C2=A0 =C2=A0 self::$oPDO =3D new PDO(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0'mysql:host=3D'.CONF::DATABASE_HO= ST_NAME.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0';dbname=3D'.CONF::DATABASE_NAME.= ';charset=3Dutf8mb4',
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CONF::DATABASE_USER_NAME,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CONF::DATABASE_PASSWORD,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[]
=C2=A0 =C2=A0 =C2=A0 );
=C2=A0 =C2=A0}
}

// Testing class in test namespace:
namespace test;
class SQLTester {

=C2=A0 =C2=A0// How do I make the SQL class see \test\CONF instead of
=C2=A0 =C2=A0// \CONF, when SQL calls for CONF in this test scope,
=C2=A0 =C2=A0/// without changing anything inside of the SQL class?
}


I think some kind of sandboxing tools would be useful for
build/test/deployment.

You could hack this out using the= autoloader, but it's something that the PHP community frowns upon, imo= . A much prevalent practice in the PHP ecosystem is a Dependency Injection = container. A somewhat similar concept exists in the Javascript ecosystem wi= th hoisting import statements and mocking modules, but if you don't und= erstand the system and are unaware that order of import execution will matt= er on whether the mock succeeds or not plays a huge role in making it a cum= bersome and awkward system.

Regardless, it's n= ot possible for functions as users don't control function autoloader.--
Marco = Deleu
--000000000000f1a99b061ebb22c1--