New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
PHP namespace headache
Can anyone explain how the hell PHP namespace actually work.
So, I have a class:
Now, the usual:
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?
Comments
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 isUser\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 replacingDB
with\DB
(also assuming it is in the root namespace).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.
Use an autoloader and then refer the
DB
class by its full namespace. For example, ifDB
's full namespace isApp\DB
then refer to it as\App\DB
when calling it. You can also useuse App\DB
at the top so that you can refer to it as justDB
elsewhere in the file.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 SQLiExample code is just a mock up . Thank you for warning.
yes
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.
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.
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.
Don't jinx it
Said by every programmer right before it goes into production.
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. ...
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.
@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.
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.
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.
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.
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.