Copy
<?php
class IPv4Address implements Stringable {
public function __construct(private string $oct1, private string $oct2, private string $oct3, private string $oct4) {
}
public function __toString(): string {
return "$this->oct1.$this->oct2.$this->oct3.$this->oct4";
}
}
?>
https://www.php.net/manual/en/class.stringable.php
활용 방법 예제 좀 부탁드려요.
답변 2개 / 댓글 3개
채택된 답변
+20 포인트
3년 전
$tmp1= 'abc';
$tmp2= new IPv4Address('123', '234', '42', '9');
$tmp3= $tmp1 . $tmp2; // 이런 것이 가능하죠.
답변에 대한 댓글 3개
3년 전
echo $tmp2; //나
prinit( $tmp2); //도 가능할 듯합니다.
__toString()이 있으면
문자열이 들어 갈 수 있는 곳에는 다 들어갈 수 있다고 보면 됩니다.
prinit( $tmp2); //도 가능할 듯합니다.
__toString()이 있으면
문자열이 들어 갈 수 있는 곳에는 다 들어갈 수 있다고 보면 됩니다.
해당 링크에 답이 있었네요. ㅎ
요런 식으로 독립변수 넣어 사용하네요.
$ip = new IPv4Address('123', '234', '42', '9');
답변을 작성하려면 로그인이 필요합니다.