While trying to develop my framework (to ease my development time), I run into this problem Fatal error: Uncaught Error: Method name must be a string in C:\xampp\htdocs\mywebsites\gbenga\classes\General\EntryPoint.php:47 Stack trace: #0 C:\xampp\htdocs\mywebsites\gbenga\public\index.php(8): General\EntryPoint->run() #1 {main} thrown in C:\xampp\htdocs\mywebsites\gbenga\classes\General\EntryPoint.php on line 47 and I can’t find any solution to work for me even here on stackover.
Did the code you are showing with things like $this->route and $this->method really come from the book? If so then time for a new book.
If not then take a step back and try to look at the posted code through the eyes of someone who has never seen it before. How exactly would they have any idea what all the $this stuff is supposed to be?
Wrong reason to write a framework, since writing one takes a lot of time initially, plus you will also need to maintain it, which costs even more time. If it’s really to ease development time you should get a framework of the shelf (like symfony, laravel or zend), learn how to work with it properly, and gain speed from that.
There are several reasons for creating your own framework, such as for educational purposes, but development speed certainly isn’t one of them.
Truly sorry to hear that. Like I said before, time for a new book. Just think about the title. Ninjas are supposed to be sneaky, unpredictable and dangerous. Good code should be straight forward, boring and safe.
I had a bit of a look around the github source code repository for that book, presuming you mean the sixth edition of PHP & MySQL Novice to Ninja, and there are lots and lots of files called Entrypoint.php. But, presuming I’ve got the correct one (in the Final-Website code branch), the code for run() isn’t the same as in the first post on here. In particular the lines that you think are causing the problem are different.
It may well be that the code has been corrected since the book was published, hence the practice of making the code available elsewhere so that amendments can be made.
after google search, I discovered it will returned the above error(undefined error)if it is not inside the isset and empty functions. That is why I included the isset and empty functions in the code.
Ah, so the isset() and empty() sections aren’t from the book as you described earlier?
The thing is, you have to use them correctly. Use them to check whether those entries exist, sure, but then don’t use the result of them - whether they come back as false or true, the result will not be what you want. Those two functions return Boolean results - true or false - so you can’t just then use those results in place of the original code. Sure, do a check to see if they exist prior to using them, but then just don’t call the next line if they do not. I don’t know what the code does, but if it doesn’t already have a check in place, it suggests that it should not be possible for them to not exist.
The book never has !empty around this line, and the addition of the empty check is the cause of your problem. empty() will return true or false. So you’re essentially doing $controller->true() and there is no method (and cant be a method) with the value true as a name.
Then var_dump the $action variable to check it contains the relevant value, it should be a string, the name of the method to call in the controller. If it’s not a string (or you get undefined index) then double check the $routes array.
If you want, go ahead and var_dump($routes) right after you get them and post the results. It’s just a big array. Might also dump out $this->route and $this->method.
This all just learning the basics of arrays. Would not be surprised at all if $routes ends up being empty.