
rick645
Members-
Posts
102 -
Joined
-
Last visited
Everything posted by rick645
-
I only did a low level test to understand how it works ok, but here we are already at a higher level They could have documented this more clearly
-
-
$ cat main.php <?php spl_autoload('FooBar'); $ cat foobar.php <?php echo basename(__FILE__) . ": LOADED" . PHP_EOL; $ php main.php foobar.php: LOADED OK!!!
-
ok but what means The autoload function being registered. If null, then the default implementation of spl_autoload() will be registered.
-
In particular https://www.php.net/manual/en/function.spl-autoload-register.php The autoload function being registered. If null, then the default implementation of spl_autoload() will be registered. In particular https://www.php.net/manual/en/function.spl-autoload.php This function is intended to be used as a default implementation for __autoload(). If nothing else is specified and spl_autoload_register() is called without any parameters then this function will be used for any later call to __autoload(). So what? Intuitively, I did some tests, but I didn't understand much $ cat main.php <?php spl_autoload('App'); var_dump(LOADED); $ cat App.php <?php define('LOADED', true); $ php main.php PHP Fatal error: Uncaught Error: Undefined constant "LOADED" in main.php:3 ...mmmhhh... It seems that the file App.php has not been loaded. In practice, what is the use of function spl_autoload()? What does he do?
-
What is it for ReflectionAttribute::IS_INSTANCEOF
rick645 replied to rick645's topic in PHP Coding Help
t hanks -
What is it for ReflectionAttribute::IS_INSTANCEOF
rick645 replied to rick645's topic in PHP Coding Help
https://www.php.net/manual/en/language.attributes.reflection.php //... $attributes = $reflection->getAttributes(); //... Do you do this, or am I wrong? This is clear to me !!! But I don't understand what function performs the constant ReflectionAttribute::IS_INSTANCEOF -
What is it for ReflectionAttribute::IS_INSTANCEOF
rick645 replied to rick645's topic in PHP Coding Help
The page talks about Attribute::TARGET_*[1] and not Attribute::TARGET Anyway, do you know what the constant ReflectionAttribute::IS_INSTANCEOF is for? $ php -r 'echo ReflectionAttribute::IS_INSTANCEOF . PHP_EOL;' 2 So what? [1] Expandable in Attribute::TARGET_CLASS Attribute::TARGET_FUNCTION Attribute::TARGET_METHOD Attribute::TARGET_PROPERTY Attribute::TARGET_CLASS_CONSTANT Attribute::TARGET_PARAMETER Attribute::TARGET_ALL Attribute::IS_REPEATABLE -
What is it for ReflectionAttribute::IS_INSTANCEOF
rick645 replied to rick645's topic in PHP Coding Help
~$ php -r 'echo Attribute::TARGET;' PHP Fatal error: Uncaught Error: Undefined constant Attribute::TARGET in Command line code:1 -
https://www.php.net/manual/en/reflectionattribute.gettarget.php Returns the target of the attribute as bitmask So what? Some examples? Cases of use?
-
Sorry, the point "." It is already used as a concatenation operator, therefore to example they can be used => CLASS[=>PROPERTY|METHOD|CONST]=>string|int:ATTRIBUTE_KEY->... CLASS[=>PROPERTY|METHOD|CONST]=>string|int:ATTRIBUTE_KEY::...
-
However too many lines of code to access an attribute. In the future they should invent simpler things. Examples CLASS[.PROPERTY|METHOD].string|int:ATTRIBUTE_KEY->... CLASS[.PROPERTY|METHOD].string|int:ATTRIBUTE_KEY::... MyClass.0->method(); MyClass.my_attrib_name->... MyClass.1::CONST MyClass.2::staticMethod(); $instance.0->method(); $instance.my_attrib_name->... $instance.1::CONST $instance.2::staticMethod(); $instance.myProperty.0->... $instance.myProperty.my_name->... $instance.myProperty.1::... $instance.myMethod().0->... $instance.myMethod().my_name->... $instance.myMethod().1::... Someone should propose this on RFCs.
-
I would like the use of the $target variable to be more limited. Here for example $attr->doSomething($target) In practice I would like the Attr class, from its interior, could automatically go back to the Target* class (or rather, to the instance), without passing it to the dosomething() method: maybe only to the construct, calling through the method newInstance() I had tried it test1.php:#[Attr(target: $this)] test2.php:#[Attr(target: self)]
-
Some improvements of compressibility tree ├── test1.php ├── test2.php └── x.php cat test1.php <?php #[Attr(target: $this)] class Target {} cat test2.php <?php #[Attr(target: self)] class Target {} cat x.php <?php #[Attribute(Attribute::TARGET_CLASS)] class Attr { function __construct(Target $target) { var_dump( $target ); } } require $argv[1]; $reflection = new ReflectionObject( new Target ); $attributes = $reflection->getAttributes( Attr::class ); foreach ($attributes as $attribute) { var_dump( $attribute->newInstance(), ); } grep -F 'target:' * test1.php:#[Attr(target: $this)] test2.php:#[Attr(target: self)] php x.php test1.php PHP Fatal error: Constant expression contains invalid operations in test1.php on line 4 php x.php test2.php PHP Fatal error: Uncaught Error: Undefined constant "self" in x.php:23
-
tree ├── test1.php ├── test2.php ├── test3.php └── x.php cat test1.php <?php #[Attr()] class Cls { function method() {} } cat test2.php <?php #[Attr($this)] class Cls { function method() {} } cat test3.php <?php #[Attr(self)] class Cls { function method() {} } cat x.php <?php #[Attribute(Attribute::TARGET_CLASS)] class Attr { function __construct(Cls $cls = null) { $cls ? $cls->method() : Cls::method() ; } } require $argv[1]; $reflection = new ReflectionObject(new Cls); $attributes = $reflection->getAttributes( Attr::class ); foreach ($attributes as $attribute) { var_dump( $attribute->newInstance(), ); } In evidence (the targets) grep -F '#[Attr(' * test1.php:#[Attr()] test2.php:#[Attr($this)] test3.php:#[Attr(self)] The problems php x.php test1.php PHP Fatal error: Uncaught Error: Non-static method Cls::method() cannot be called statically in x.php:8 php x.php test2.php PHP Fatal error: Constant expression contains invalid operations in test2.php on line 4 php x.php test3.php PHP Fatal error: Uncaught Error: Undefined constant "self" in x.php:22 Solution?
-
https://www.amitmerchant.com/reasons-use-composition-over-inheritance-php/ ok maybe it's right, in my case, to use inheritance
-
Many say they avoid inheritance and to encourage composition. But with the exceptions, how do you do it? The following example, how would you modify it to eliminate the smell codes? new class('OTHER', 'MESSAGE', 123) extends InvalidArgumentException {// SMELL CODE function __construct(private $otherDetails, ...$params) { parent::__construct(...$params);// SMELL CODE } }; Something similar could be tempted new class('OTHER', new InvalidArgumentException('MESSAGE', 123)) implements InvalidArgumentException { function __construct(private $otherDetails, private InvalidArgumentException $wrapped) { } }; but there would be problems already in the compilation phase PHP Fatal error: InvalidArgumentException@anonymous cannot implement InvalidArgumentException - it is not an interface
-
[vscode] PHPDoc Completion
rick645 replied to rick645's topic in Editor Help (PhpStorm, VS Code, etc)
When I try to log in, after a while it appears https://pasteboard.co/BP1ISrgp00nt.png -
[vscode] PHPDoc Completion
rick645 replied to rick645's topic in Editor Help (PhpStorm, VS Code, etc)
https://pasteboard.co/TVurPfsE41JD.png https://pasteboard.co/ohvicwr7Gdbx.png -
https://docs.devsense.com/en/vscode/editor/phpdoc But why doesn't it work for me? https://pasteboard.co/0UoiRUGepDXO.png
-
I had a look but I didn't really understand how to use it https://github.com/gmrchk/cli-testing-library#usage You need to create a file of this type import { prepareEnvironment } from '@gmrchk/cli-testing-library'; describe('My CLI', () => { it('program runs successfully', async () => { const { execute, cleanup } = await prepareEnvironment(); const { code } = await execute( 'node', './my-cli.js --help' ); expect(code).toBe(0); await cleanup(); }); }); and then where should we save it? Since they are tools written in JS (if I'm not mistaken), do the tests have to be launched from the browser? PS Do you know anything about this topic? https://forums.phpfreaks.com/topic/317297-vscode-docblock-and-tag-see/#comment-1612101 No one has responded yet....
-
UPPPPPPPPPPPP
-
vscode, docblock and tag @see
rick645 replied to rick645's topic in Editor Help (PhpStorm, VS Code, etc)
UPPPPPPPPPPPPPPPPPPPPPPPPPPP -
PHP Intelephense extension installed. Cursor positioned in line 4, column 15. Press Ctrl+Shift+P, type "go to definition", and it appears "Non è stata trovata alcuna definizione…" (No definition has been found ...) https://pasteboard.co/7kEtT0JyS8Sk.png Why?
-
As we know, to perform the unit tests in general is used phpunit https://phpunit.de/ But for functional tests? For example, let's consider the following script + cat bin/example-script #!/usr/bin/php <?php require "src/App.php"; /** * @return string Application output. */ function main(): string { $app = new App; // --- BEGIN CONFIG --- $app->setProperty1( // ... ); $app->setProperty2( // ... ); $app->setProperty3( // ... ); // --- END CONFIG --- $output = $app->run(); # RETURN: "Helo world!!!" return $output; } echo main() . PHP_EOL; # ECHO: "Helo world!!!" . PHP_EOL exit(123); # EXIT CODE: 123 We highlight some lines + grep '# ' bin/example-script $output = $app->run(); # RETURN: "Helo world!!!" echo main() . PHP_EOL; # ECHO: "Helo world!!!" . PHP_EOL exit(123); # EXIT CODE: 123 Request 1 I want to test that the output is the desired one: "Helo world!!!" . PHP_EOL Request 2 I want to test that the exit code is the desired one: 123 How can it be done? Is there a recommended practice?