![]() |
|
[PHP] [OOP]Method function not calling object in Class - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183) +---- Thread: [PHP] [OOP]Method function not calling object in Class (/showthread.php?tid=6070044) |
[OOP]Method function not calling object in Class - OneandonlySunny™ - Apr 21, 2020 ![]() i am calling ceo->getname function method , its generating error too few arguments on the other hand getsalary calling is working here, i am learning this from Beginning PHP and MySQL From Novice to Professional (Authors: Gilmore, W Jason) EBOOK. Could u kindly explain this matter to me & do correction in my error here's the code if u want to run. PHP Code RE: [OOP]Method function not calling object in Class - Exalted - Apr 22, 2020 This should work: PHP Code Your code didn't work because you specified a signed decimal integer with %d, but getName() returns a string, so you should use %s. However, you did not give any arguments as you used dots instead of commas. I suggest that you check out the printf reference on php.net. RE: [OOP]Method function not calling object in Class - Backbone - Apr 22, 2020 On your printf, you use %d instead of %s. Refer to the above comment. RE: [OOP]Method function not calling object in Class - OneandonlySunny™ - Apr 22, 2020 (Apr 22, 2020 12:44 AM)Exalted Wrote: This should work: (Apr 22, 2020 01:03 AM)Backbone Wrote: On your printf, you use %d instead of %s. Refer to the above comment. thanks , i understand . but i got a question This one works with dot(.) Code printf("Bunny's Salary is : ".$ceo->getsalary()."\n<br/>");but this one doesn't , why ? Code printf("Name is : %d".$ceo->getname());RE: [OOP]Method function not calling object in Class - Nachfolger - Apr 22, 2020 (Apr 22, 2020 07:29 AM)OneandonlySunny™ Wrote: thanks , i understand . but i got a questionYou don't seem to be understanding this at all. A period (.) concatenates two strings together. A comma (,) is a parameter separator (in THIS case). When you provide a placeholder for printf, you MUST provide the variable to replace the placeholder with. Also, I'm fairly certain you were already told not to use %d for emp::getname. PHP Code By the way, it's 2020... PLEASE USE MODERN CAPABILITIES. PHP Code PS, salary being a string makes zero sense whatsoever and your function names make me wanna die. With love, Nachfolger. RE: [OOP]Method function not calling object in Class - Charger - Apr 22, 2020 (Apr 22, 2020 07:29 AM)OneandonlySunny™ Wrote: thanks , i understand . but i got a questionprintf takes a formated string (ex hello %s where %s is world) You have a couple options: 1. change all of your printf to print or echo and to print multiple strings just concat them with a "." 2. change all of your printf to have string placeholders (%s) in them. RE: [OOP]Method function not calling object in Class - youlded - has1 - Apr 22, 2020 printf is a function that takes a format (hence why its called f) and replaces the placeholders in this format for actual values. %d is replaced by a numeric argument, %s is replaced by a string, and so on. You can take a look at all the options here: https://www.php.net/manual/en/function.printf.php (Apr 22, 2020 07:29 AM)OneandonlySunny™ Wrote: This one works with dot(.) Yes, that works because you first concatenate two strings and then print format it. You did not specify any placeholders (%s, %d, etc), so there is nothing to replace and the format function is just printing your format as-is (Apr 22, 2020 07:29 AM)OneandonlySunny™ Wrote: but this one doesn't , why ? What you are doing here is you are calling the printf function with this format Code Name is : %d BunnieYou have 1 placeholder, but you are not providing the argument that this placeholder should be replaced with. You need to provide this 1 argument that this placeholder will be replaced with RE: [OOP]Method function not calling object in Class - Disease - Apr 22, 2020 OP sends me a PM two days ago asking this very question. I point out the error being that they mistyped a period where it should be a comma to separate parameters in the printf() function. No response. Now I look and a day after I responded to his PM, he makes this thread and demonstrates a general unwillingness to properly read the responses. Much as he did with my PM. This, ladies and gentlemen, is textbook on why we don't answer PMs asking for help. I should've ignored his PM like I have done with basically every other one I've received over the years. RE: [OOP]Method function not calling object in Class - Charger - Apr 22, 2020 (Apr 22, 2020 05:36 PM)Disease Wrote: This, ladies and gentlemen, is textbook on why we don't answer PMs asking for help. I should've ignored his PM like I have done with basically every other one I've received over the years. 💯💯💯💯 Nachfolger and I get messages all the frickin time. RE: [OOP]Method function not calling object in Class - OneandonlySunny™ - Apr 22, 2020 Thanks everyone , i clearly understand now this matter . Although i am practicing & learning, if i get stuck again i would come to get your help again. Thanks again. |