Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

[OOP]Method function not calling object in Class

[Image: yMqkgqT.png]

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

<?php 
class emp2{
        private 
$name;
        private 
$salary;
        
        function 
setname($name){
            
$this->name = $name;
        }
        function 
getname(){
            return 
$this->name;
        }
        
        function 
setsalary($salary){
            
$this->salary = $salary;
        }   
        function 
getsalary(){
            return 
$this->salary;
        }
    }
    class 
executive1 extends emp2{
        function  
pillagecompany(){
            
$this->setsalary($this->getsalary() * 10);
        }
    
    }
    class 
ceo extends executive1{
        function 
facelift(){
            echo 
"<br/>Nip Nip tuck tuck!";
        }
    }
    
    
$ceo = new ceo();
    
$ceo->setname("Bunnie");
    
$ceo->setsalary("50000");
    
$ceo->pillagecompany();
    
$ceo->facelift();
    
printf("Bunny's Salary is : ".$ceo->getsalary()."\n<br/>");
    
printf("Name is : %d".$ceo->getname()); 
This should work:
PHP Code

<?php 
printf
("Bunny's Salary is : %d", $ceo->getsalary() . "\n<br />");
printf("Name is : %s", $ceo->getname()); 

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.
On your printf, you use %d instead of %s. Refer to the above comment.
(Apr 22, 2020 12:44 AM)Exalted Wrote:  This should work:
PHP Code

<?php 
printf
("Bunny's Salary is : %d", $ceo->getsalary() . "\n<br />");
printf("Name is : %s", $ceo->getname()); 

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.

(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());
(Apr 22, 2020 07:29 AM)OneandonlySunny™ Wrote:  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());
You 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

<?php 
printf
("Name is : %s", $ceo->getname()); // Name is : <name> 
I SERIOUSLY urge you to read the documentation on printf. Especially when you're copying/pasting code that you don't understand.

By the way, it's 2020... PLEASE USE MODERN CAPABILITIES.
PHP Code

<?php 
class emp2 {
   private $name;
   private $salary;
   
   
/**
    * Set employee name
    * 
    * @param string name
    * @return none
    * @throws TypeError
   */
   public function setname(string $name) : void {
       $this->name = $name;
   }
   
   
/**
    * Get employee name
    * 
    * @param none
    * @return string name
    * @throws TypeError
   */
   function getname() : string {
       return $this->name;
   }
   
   
/**
    * Set employee salary
    * 
    * @param string salary
    * @return none
    * @throws TypeError
   */
   function setsalary(string $salary) : void {
       $this->salary = $salary;
   }   
   
   
/**
    * Get employee salary
    * 
    * @param none
    * @return string salary
    * @throws TypeError
   */
   function getsalary() : string {
       return $this->salary;
   }
} 
* Typehinting

PS, salary being a string makes zero sense whatsoever and your function names make me wanna die.

With love,
Nachfolger.
Je t'aime
[Image: fpZ5a5g.png]
(Apr 22, 2022 04:39 PM)-Scrub Wrote:  Jokes on you, I'm not smart enough to know what he's talking about.
(Apr 22, 2020 07:29 AM)OneandonlySunny™ Wrote:  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());
printf 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.
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(.)
Code
 printf("Bunny's Salary is : ".$ceo->getsalary()."\n<br/>");

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 ?

Code
printf("Name is : %d".$ceo->getname());


What you are doing here is you are calling the printf function with this format
Code
Name is : %d  Bunnie

You 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
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.
<Disease />
(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.
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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by okonoidMar 6, 2021 04:30 AM 3 199 Mar 6, 2021 04:30 AM
Last Post: mahadev
[Javascript]  by SмℯґƒDec 7, 2020 09:57 PM 2 130 Dec 7, 2020 09:57 PM
Last Post: Sмℯґƒ
[PHP] by bull daemonDec 10, 2018 04:23 AM 4 188 Dec 10, 2018 04:23 AM
Last Post: northerncrisp