Created
March 11, 2015 21:14
-
-
Save tadeubdev/bb5056b9013c410daaf5 to your computer and use it in GitHub Desktop.
Html Class PHP: Html::span("It's a test!") // prints: <span>It's a test!</span>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Html | |
{ | |
public static $content; | |
public static $name; | |
public static $attrs = []; | |
/** | |
* For uses: Html::span('sample') | |
* @param str $meth Name of element | |
* @param str $args Content | |
* @return static item | |
*/ | |
public static function __callStatic( $meth, $args ) | |
{ | |
self::$name = $meth; | |
self::$content = isset($args[0])? $args[0]: null; | |
return new static; | |
} | |
/** | |
* Get Html::span('sample')->title('My title('My title') | |
* @param str $meth Name | |
* @param str $args Arguments | |
* @return static item | |
*/ | |
public function __call( $meth, $args ) | |
{ | |
self::$attrs[ $meth ] = $args[0]; | |
return new static; | |
} | |
/** | |
* Prints data | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
$attrs = ''; | |
if( count( self::$attrs ) ) | |
{ | |
$attrs = ' '; | |
foreach( self::$attrs as $key => $value ) | |
{ | |
$attrs .= "{$key}=\"{$value}\" "; | |
} | |
$attrs = substr( $attrs, 0, strlen( $attrs ) - 1 ); | |
} | |
return sprintf("<%s%s>%s</%s>", self::$name, $attrs, self::$content, self::$name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment