Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Advertise on LowEndTalk.com
PHP namespace headache
New on LowEndTalk? Please Register and read our Community Rules.

PHP namespace headache

LTnigerLTniger Member

Can anyone explain how the hell PHP namespace actually work.

So, I have a class:

https://pastebin.com/Mi4BQdWz

Now, the usual:

https://pastebin.com/5AdrkY99

Gives me a nice error:

Fatal error: Uncaught Error: Class 'User\tools\DB' not found

Am I insane? Why namespace prefixes other class (DB) too? How to avoid this?

#!/Bashblog.net | Free Wordpress Hosting | If you can't idle, what's the point?

Comments

  • jmgcaguiclajmgcaguicla Member
    edited March 25

    I haven't written PHP in ages, but it looks like it's looking for DB inside the current namespace where you call it which is User\tools.

    I'm assuming you're using an autoloader which would mean DB not existing is not the problem which then leads me to suggest that you try replacing DB with \DB (also assuming it is in the root namespace).

  • CConnerCConner Member, Provider
    edited March 25

    The DB class doesn't seem to be included anywhere. Make sure to either specify the full path of the class or include it, otherwise, it will try and look for the DB class in the current namespace, which is User\Tools and that will, obviously, fail.

    GameDash, an AIO solution uniting billing, support & game server management.
    Visit our website or join our Discord to find out more.

  • edited March 25

    Use an autoloader and then refer the DB class by its full namespace. For example, if DB's full namespace is App\DB then refer to it as \App\DB when calling it. You can also use use App\DB at the top so that you can refer to it as just DB elsewhere in the file.

    Thanked by 1Daniel15
  • Daniel15Daniel15 Member
    edited March 25

    by the way, your user class has an SQL injection vulnerability... Please use PDO (rather than that custom DB class) with prepared statements (https://www.php.net/manual/en/pdo.prepare.php) which will help you avoid SQLi

  • LTnigerLTniger Member

    @Daniel15 said:
    by the way, your user class has an SQL injection vulnerability... Please use PDO (rather than that custom DB class) with prepared statements (https://www.php.net/manual/en/pdo.prepare.php) which will help you avoid SQLi

    Example code is just a mock up :) . Thank you for warning.

    Thanked by 1webcraft

    #!/Bashblog.net | Free Wordpress Hosting | If you can't idle, what's the point?

  • Jona4sJona4s Member

    @LTniger said:
    PHP actually work.
    Am I insane?

    yes

  • nemnem Member, Provider
    edited March 26

    Composer includes a nice autoloader. Use that unless you want to implement your own or manually include each class you're referencing via require/include/require_once/include_once.

    Thanked by 1LTniger
  • raindog308raindog308 Administrator, Moderator

    I came here expecting a lot of cheap PHP shots and PHP bashing. Instead, people are giving solid advice. I guess LET really is going downhill... :'(

    Thanked by 2Jona4s kkrajk

    For LET support, please visit the support desk.

  • @raindog308 said: I came here expecting a lot of cheap PHP shots and PHP bashing. Instead, people are giving solid advice. I guess LET really is going downhill... :'(

    Hot takes and angry rants aren't actually good for the growth of a forum. It only serves to provide entertainment to older members.

  • @raindog308 said:
    I came here expecting a lot of cheap PHP shots and PHP bashing. Instead, people are giving solid advice. I guess LET really is going downhill... :'(

    PHP isn't the same as it was a few years back. A lot of things have been changed.

    @LTniger what is the location path of your files? Looks like PHP can't find them.

    Always trying to be negative ^^.

  • @raindog308 said:
    I came here expecting a lot of cheap PHP shots and PHP bashing. Instead, people are giving solid advice. I guess LET really is going downhill... :'(

    Don't jinx it

  • @LTniger said:

    @Daniel15 said:
    by the way, your user class has an SQL injection vulnerability... Please use PDO (rather than that custom DB class) with prepared statements (https://www.php.net/manual/en/pdo.prepare.php) which will help you avoid SQLi

    Example code is just a mock up :) .

    Said by every programmer right before it goes into production.

  • Jona4sJona4s Member
    edited March 27

    except there are no PHP "programmers".

    more like PHP script coders.

    Programming languages are generally compiled and create an executable file, whereas Scripting languages are interpreted and do not create an executable file. ...

  • LTnigerLTniger Member

    Community, this time I have received real help. Thanks to @nem I have deployed composer and it's autoloading feature. Thank you!

    Now you may derail discussion as usual.

    Thanked by 1nem

    #!/Bashblog.net | Free Wordpress Hosting | If you can't idle, what's the point?

  • tommmytommmy Member

    @LTniger why did you remove the code example? It can help people to learn. I wanted to learn.

  • LTnigerLTniger Member

    @tommmy said:
    @LTniger why did you remove the code example? It can help people to learn. I wanted to learn.

    Sorry, it was my default pastebin settings. To learn from my misstake is just autoload your classes instead of include hell. PSR-4 all the way.

    #!/Bashblog.net | Free Wordpress Hosting | If you can't idle, what's the point?

  • 0xbkt0xbkt Member

    @Jona4s said:
    except there are no PHP "programmers".

    more like PHP script coders.

    Programming languages are generally compiled and create an executable file, whereas Scripting languages are interpreted and do not create an executable file. ...

    They are not mutually exclusive. Plus, HHVM and, say, V8 does JIT compilations behind the scene. Dart, for instance, didn't initially support AOT compilation which later arrived. So, do you say Dart was a "scripting language" before it could output a single executable file in which time it became a "programming language"? Totally irrelevant.

    Thanked by 1Daniel15
  • Jona4sJona4s Member
    edited March 27

    @0xbkt said:
    They are not mutually exclusive. Plus, HHVM and, say, V8 does JIT compilations behind the scene. Dart, for instance, didn't initially support AOT compilation which later arrived. So, do you say Dart was a "scripting language" before it could output a single executable file in which time it became a "programming language"? Totally irrelevant.

    Right, that's why scripting languages users are not real programmers. They will never need to worry about single stepping, setting breakpoints, or inspect the codepaths of an objdump.

    You cannot trace syscalls, probe user/kernel traps, sample L1 missess, perf cpu stalled code, or trap expensive allocations in an IR language, because all you will see will be the VM symbols.

    Interpreted languages hide all the complexities that make a computer program, and turn them into a syntax sugar ergonomic abstraction so that **** you don't have to worry about any real programming.

    You think you are cool because you type javascript syntax, and 8 million lines of V8 does all the heavy work of things you will never ever need to touch.

  • LTnigerLTniger Member

    @Jona4s said:
    You think you are cool because you type javascript syntax, and 8 million lines of V8 does all the heavy work of things you will never ever need to touch.

    Yes, if that pays my bills :) . In the mean time you can create website with asm and be the coolest dude in the basement, I don't mind. So, keep your compilers, decompilers, stepings, shifting and tracings to your-self.

    Thanked by 1Jona4s

    #!/Bashblog.net | Free Wordpress Hosting | If you can't idle, what's the point?

  • @Jona4s said:
    You cannot trace syscalls,

    Says who? Strace works fine with many JS runtimes (eg https://just.billywhizz.io/blog/on-javascript-performance-02/) and V8 has the ability to print out the assembly code generated by its JIT compiler (eg https://stackoverflow.com/questions/277423/how-can-i-see-the-machine-code-generated-by-v8).

  • Jona4sJona4s Member

    @Daniel15 said:

    @Jona4s said:
    You cannot trace syscalls,

    Says who? Strace works fine with many JS runtimes (eg https://just.billywhizz.io/blog/on-javascript-performance-02/) and V8 has the ability to print out the assembly code generated by its JIT compiler (eg https://stackoverflow.com/questions/277423/how-can-i-see-the-machine-code-generated-by-v8).

    By tracing I meant putting a bp on the user code before a syscall instruction, and stepping/tracing the stack unwinding.

    You cannot do that on javascript because again, you will step into VM code which you cannot modify.

    Example in an Go elf.

     TEXT main.main(SB) /main.go
     main.go:1830       0x6d4d60        64488b0c25f8ffffff  MOVQ FS:0xfffffff8, CX                      
     main.go:1830       0x6d4d69        483b6110        CMPQ 0x10(CX), SP                       
     main.go:1832       0x6d4d6d        0f05            SYSCALL 
    

    By looking at the SSA and only ~6000 lines runtime, you can easily follow the subroutines and step/trace into syscalls to produce consistent disassembly.

    V8 however, might produce different disassembly even if the javascript code is the same, because you are not in control of the elf and cannot step into nor modify it, because it's the VM and not your code.

Sign In or Register to comment.