Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MyString{
- function __construct($string){
- $this->storage = $string;
- }
- function substr($from,$len){
- return new static(substr($this->storage, $from, $len));
- }
- static function __fromScalar($value){
- return new static($value);
- }
- }
- function test(MyString $value){
- echo $value->substr(0,5);
- }
- test("hello world"); // implicitly constructs the object via MyString::__fromScalar() and prints "hello"
- // ---------------------------------------------------------------------
- class Color
- {
- function __construct($red, $green, $blue, Integer $alpha = 1){
- // ...
- }
- static function newFromHex(String $string){
- // ...
- }
- static function newFromHSV(...){
- // ...
- }
- static function newFromHSL(...){
- // ...
- }
- static function __fromScalar($value){
- if(preg_match("@^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$@i", $value))
- return static::newFromHex($value);
- elseif(preg_match(...))
- return static::newFromHSL(.....);
- elseif(preg_match(...))
- return static::newFromHSV(.....);
- // ...
- else
- throw new Exception("couldn't parse the color string");
- }
- function setAlpha(Integer $alpha){
- $this->alpha = $alpha;
- }
- function __toString(){
- // ...
- }
- }
- function test(Color $color){
- return $color->setAlpha(0.9);
- }
- test("#ff0000"); // prints rgba(255,0,0,0.9)
- test("rgb(255,0,0)"); // prints rgba(255,0,0,0.9)
- // ---------------------------------------------------------------------
- // also:
- // method return type:
- function ReturnTypeClass test(String $test){
- return "hello"; // correct, equals to write "return ReturnTypeClass::__fromScalar("hello");"
- }
- class ReturnTypeClass{
- static function __fromScalar($value){
- return new self($value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement