Jump to content

rick645

Members
  • Posts

    107
  • Joined

  • Last visited

Everything posted by rick645

  1. For every problem, we always tend to look for the simplest solution $solution1 = 2 * 5; $solution2 = 2 + 2 + 2 + 2 + 2; for ($i = 1; $i <= 5; $i++) { $solution3 = $i * 2 . PHP_EOL; } Which do you prefer?
  2. For every problem, we always tend to look for the simplest solution $solution1 = 2 * 5; $solution2 = 2 + 2 + 2 + 2 + 2; for ($i = 1; $i <= 5; $i++) { $solution3 = $i * 2 . PHP_EOL; } Which do you prefer?
  3. I was thinking of doing it with a single instruction
  4. dir$ pwd /tmp/tmp.xWJxJWnltu/dir dir$ tree . ├── a ├── b └── c 0 directories, 3 files dir$ cat ../test.php <?php var_dump( 'OUTPUT1', new RecursiveDirectoryIterator( getcwd() ) ); var_dump( 'OUTPUT2', iterator_to_array( new RecursiveDirectoryIterator( getcwd() ) ) ); var_dump( 'OUTPUT3', iterator_to_array( new RecursiveDirectoryIterator( getcwd(), FilesystemIterator::CURRENT_AS_PATHNAME ) ) ); var_dump( 'OUTPUT4', iterator_to_array( new RecursiveDirectoryIterator( getcwd(), FilesystemIterator::CURRENT_AS_PATHNAME . FilesystemIterator::SKIP_DOTS ) ) ); dir$ php ../test.php /tmp/tmp.xWJxJWnltu/test.php:5: string(7) "OUTPUT1" /tmp/tmp.xWJxJWnltu/test.php:5: class RecursiveDirectoryIterator#1 (4) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/." private $fileName => string(1) "." private $glob => bool(false) private $subPathName => string(0) "" } /tmp/tmp.xWJxJWnltu/test.php:12: string(7) "OUTPUT2" /tmp/tmp.xWJxJWnltu/test.php:12: array(5) { '/tmp/tmp.xWJxJWnltu/dir/.' => class SplFileInfo#3 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/." private $fileName => string(1) "." } '/tmp/tmp.xWJxJWnltu/dir/c' => class SplFileInfo#4 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/c" private $fileName => string(1) "c" } '/tmp/tmp.xWJxJWnltu/dir/..' => class SplFileInfo#5 (2) { private $pathName => string(26) "/tmp/tmp.xWJxJWnltu/dir/.." private $fileName => string(2) ".." } '/tmp/tmp.xWJxJWnltu/dir/b' => class SplFileInfo#6 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/b" private $fileName => string(1) "b" } '/tmp/tmp.xWJxJWnltu/dir/a' => class SplFileInfo#7 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/a" private $fileName => string(1) "a" } } /tmp/tmp.xWJxJWnltu/test.php:21: string(7) "OUTPUT3" /tmp/tmp.xWJxJWnltu/test.php:21: array(5) { '/tmp/tmp.xWJxJWnltu/dir/.' => string(25) "/tmp/tmp.xWJxJWnltu/dir/." '/tmp/tmp.xWJxJWnltu/dir/c' => string(25) "/tmp/tmp.xWJxJWnltu/dir/c" '/tmp/tmp.xWJxJWnltu/dir/..' => string(26) "/tmp/tmp.xWJxJWnltu/dir/.." '/tmp/tmp.xWJxJWnltu/dir/b' => string(25) "/tmp/tmp.xWJxJWnltu/dir/b" '/tmp/tmp.xWJxJWnltu/dir/a' => string(25) "/tmp/tmp.xWJxJWnltu/dir/a" } /tmp/tmp.xWJxJWnltu/test.php:31: string(7) "OUTPUT4" /tmp/tmp.xWJxJWnltu/test.php:31: array(3) { '/tmp/tmp.xWJxJWnltu/dir/c' => class SplFileInfo#5 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/c" private $fileName => string(1) "c" } '/tmp/tmp.xWJxJWnltu/dir/b' => class SplFileInfo#4 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/b" private $fileName => string(1) "b" } '/tmp/tmp.xWJxJWnltu/dir/a' => class SplFileInfo#3 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/a" private $fileName => string(1) "a" } } The output that comes closest to my expectations is OUTPUT4 (everything else is to be discarded). But actually I would like something like this array(3) { [0] => string(25) "/tmp/tmp.xWJxJWnltu/dir/c" [1] => string(25) "/tmp/tmp.xWJxJWnltu/dir/b" [2] => string(25) "/tmp/tmp.xWJxJWnltu/dir/a" } The simplest output!!! Is it possible?
  5. Thanks for the info, even if I already knew these things
  6. 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
  7. $ cat main.php <?php spl_autoload('FooBar'); $ cat foobar.php <?php echo basename(__FILE__) . ": LOADED" . PHP_EOL; $ php main.php foobar.php: LOADED OK!!!
  8. ok but what means The autoload function being registered. If null, then the default implementation of spl_autoload() will be registered.
  9. 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?
  10. 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
  11. 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
  12. ~$ php -r 'echo Attribute::TARGET;' PHP Fatal error: Uncaught Error: Undefined constant Attribute::TARGET in Command line code:1
  13. https://www.php.net/manual/en/reflectionattribute.gettarget.php Returns the target of the attribute as bitmask So what? Some examples? Cases of use?
  14. 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::...
  15. 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.
  16. 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)]
  17. 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
  18. 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?
  19. https://www.amitmerchant.com/reasons-use-composition-over-inheritance-php/ ok maybe it's right, in my case, to use inheritance
  20. 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
  21. When I try to log in, after a while it appears https://pasteboard.co/BP1ISrgp00nt.png
  22. https://pasteboard.co/TVurPfsE41JD.png https://pasteboard.co/ohvicwr7Gdbx.png
  23. https://docs.devsense.com/en/vscode/editor/phpdoc But why doesn't it work for me? https://pasteboard.co/0UoiRUGepDXO.png
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.