"PHP" / Говнокод #25182 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
<?php
class std {
    public static function cout() {
        return new std();
    }
    public function endl() {
        echo PHP_EOL;
    }
    public function __get($name) {
        echo $name;
        return $this;
    }
}

std::cout()->{'Кукареку!'}->endl();

Кукареку!

https://ideone.com/g95mdk

Запостил: nemyx nemyx, (Updated )

Комментарии (9) RSS

    • Можно, но только с расширением operator:
      http://pecl.php.net/package/operator

      <?php
      class std {
          const endl = PHP_EOL;
          public static function cout() {
              return new std();
          }
          public function __sl($val) {
              echo $val;
              return $this;
          }
      }
      
      std::cout() << 'Кукареку!' << std::endl;


      К сожалению, его нет в Ideone.
      Ответить
      • > А оператор << можно перегрузить?

        Use Perl*, Luke:
        #!/usr/bin/perl
        
        use strict;
        use warnings;
        no warnings qw(void);
        
        package std::__cout;
        
        use overload '<<' => \&__print_shl;
        
        sub __print_shl {
          my ($self, @strings) = @_;
          print @strings;
          return $self;
        }
        
        package std;
        
        sub cout {
          return bless {} => 'std::__cout';
        }
        
        sub endl {
          return "\n";
        }
        
        package main;
        
        std::cout() << "Hello, Perl" << std::endl;


        https://wandbox.org/permlink/CWq65ohMlYE1ioYR

        * Правда heredoc-syntax не позволяет пропустить скобочки после std::cout.
        Ответить
        • Точно, есть же прототипы
          #!/usr/bin/perl
          
          use strict;
          use warnings;
          no warnings qw(void);
          
          package std;
          
          use overload '<<' => \&__print_shl;
          
          sub __print_shl {
            my ($self, @strings) = @_;
            print {$self->{_handle}} @strings;
            return $self;
          }
          
          sub cout() {
            return bless { _handle => \*STDOUT } => __PACKAGE__;
          }
          
          sub cerr() {
            return bless { _handle => \*STDERR } => __PACKAGE__;
          }
          
          sub endl() {
            return "\n";
          }
          
          package main;
          
          std::cout << "Hello, Perl!" << std::endl;

          https://wandbox.org/permlink/j6nG2fmRwb3V3xgr
          Ответить
  • 0 VALUE (})
    
    : }  (}) EXECUTE ;
    
    : (end-namespace)  PREVIOUS DEFINITIONS ;
    
    : namespace
       WORDLIST CREATE DUP ,
       >R GET-ORDER R> SWAP 1+ SET-ORDER DEFINITIONS
       BL WORD COUNT 1 <> SWAP C@ [CHAR] { <> OR ABORT" syntax error"
       ['] (end-namespace) TO (})
    
       DOES> @ ' EXECUTE
    ;
    
    : ::
       >R GET-ORDER R> 1 SET-ORDER
       ' >R SET-ORDER R> EXECUTE
    ;
    
    : >SHL ;
    
    : <<  >R ' EXECUTE R@ >SHL @ EXECUTE R> ;
    
    : S'  [CHAR] ' PARSE >R HERE R@ CMOVE  HERE R> ;
    
    namespace std {
        CREATE cout ' TYPE ,
        CREATE (endl) 10 C,
        : endl  (endl) 1 ;
    }
    
    std :: cout << S' Именно поэтому я за "Forth"' << std :: endl
    https://ideone.com/XKTbZ7
    Ответить
    • Какой язык высококобенной!

      > GET-ORDER R> SWAP 1+ SET-ORDER DEFINITIONS
      > CREATE (endl) 10 C
      ЗОЖИМАЕМ> ВОРЕЦИИ 1+ ЛЕСНЫЕ КОБЕНАЦИИ
      ПОЛУЧАЕМ (вдруг) 10 БАТ
      Ответить
    • Ко-ко-ко, зачем так сложно?
      : std::cout<<"Кукурику!"<<std::endl;    ." Кукурику!" cr ;
      Ответить

Добавить комментарий

Переведи на "PHP", guest!

    А не использовать ли нам bbcode?


    8