Jump to content

Type of variables

- - - - -

  • Please log in to reply
4 replies to this topic

#1
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Hi, I have some problem with access type of variable in some method. My code look like this:

<?php


class SomeClass {

	

	public $comment;

	

	public function setComment($comment)

	{

		$this->comment = (string) $comment;

		return $this;

	}	

	

}


$a = new SomeClass();


echo $a->setComment('adi');


?>


I have written it to see what it will print me but I have receive some error:

PHP Catchable fatal error:  Object of class SomeClass could not be converted to string in /home/adrian/www/test/returnthis.php on line 17

Can you explain me it?

#2
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
and what minds: return $this in some set method (for instance setComment() )

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hi, by returning $this you are returning the object $a which is an instance of SomeClass, rather than a string. You can use the magic method __toString:
<?php

class SomeClass {
    
    public $comment;
    
    public function __toString()
    {
        return $this->comment;
    }

    public function setComment($comment)
    {
        $this->comment = (string) $comment;
        return $this;
    }    
    
}

$a = new SomeClass();

echo $a->setComment('adi'); //returns "adi"

?>

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
AdrianWierciochPHP

AdrianWierciochPHP

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
so when I will add array by some method I have to use magic method __toArray();
I saw some ModelClass which don`t use any magic method but use something like it:
$this->comment = (string) $comment

(string) is checking variable is string? What will happen when I insert in variable array or integer?

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I think you are confusing on what your class is supposed to do. By returning $this you are returning an object, why were you wanting this? were you?

If you want to return something, I believe you are wanting to return the argument, such as this:
    public function setComment($comment)
    {
        $this->comment = $comment;
        return $comment; 
    }   
Although I am not sure where you are going with that.

Quote

I saw some ModelClass which don`t use any magic method but use something like it:
$this->comment = (string) $comment
I am sure you have confused what they did, your (string) will type cast a string to a string, and PHP will handle integer to string conversion automatically anyway. The (string) is not needed, you can place an integer, string or array into $comment any way you like.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users