xangelo Posted December 23, 2010 Share Posted December 23, 2010 I've been working with various strongly typed languages rather exclusively for a little while now and was hoping to work with the same features in PHP. I understand that PHP isn't inherently typed but I've come up with some sort of pseudo-typing mechanism outlined below. I was just wondering what your thoughts were on this? class TypeException extends Exception {} class Account { private $ID; private $Name; private $Password; private $TypeEnforce; public function __construct() { $this->TypeEnforce = array( 'ID' => function($x){return is_numeric($x);}, 'Name' => function($x){ return is_string($x); }, 'Password' => function($x){ return is_string($x); } ); } public function __get($key) { return $this->$key; } public function __set($key,$value) { if(property_exists($this,$key)) { if($this->TypeEnforce[$key] != null) { // Type is enforced if($this->TypeEnforce[$key]($value)) { $this->$key = $value; } else { throw new TypeException('Type Exception: '. $key .' failed to be set to '. $value); } } } } } try { $Account = new Account(); $Account->ID = "49ga"; $Account->Name = 'Angelo'; $Account->Password = '49g10fc0q0weq'; } catch(TypeException $e) { echo $e->getMessage(); } Quote Link to comment https://forums.phpfreaks.com/topic/222522-thoughts-on-strongly-typed-implementation-for-php/ Share on other sites More sharing options...
shlumph Posted December 23, 2010 Share Posted December 23, 2010 I don't see anything wrong with validating object vars like that for class variables. That way you know exactly what's in the object vars and there will probably be less bugs. When passing objects into a function, I always use type checking like: public function add(User $user) { } I think this prevents bugs from happening down the road, and keeps things more neat. Quote Link to comment https://forums.phpfreaks.com/topic/222522-thoughts-on-strongly-typed-implementation-for-php/#findComment-1150877 Share on other sites More sharing options...
xangelo Posted December 23, 2010 Author Share Posted December 23, 2010 I've been doing the same for Objects, but primitive types aren't supported by PHP's type hinting (yet?). Quote Link to comment https://forums.phpfreaks.com/topic/222522-thoughts-on-strongly-typed-implementation-for-php/#findComment-1150879 Share on other sites More sharing options...
KevinM1 Posted December 23, 2010 Share Posted December 23, 2010 The biggest problem I see is with maintenance. With your current example, you'd have to write a valid set of metadata for every single object in your system. Further, if your design changes, you'll have to go into each affected object and edit its metadata manually. That will quickly grow tedious. See if you can refactor. @shlumph - type hinting only works with objects and arrays. It doesn't work on primitives (integers, strings, etc.). See: http://php.net/manual/en/language.oop5.typehinting.php. Quote Link to comment https://forums.phpfreaks.com/topic/222522-thoughts-on-strongly-typed-implementation-for-php/#findComment-1150946 Share on other sites More sharing options...
xangelo Posted December 24, 2010 Author Share Posted December 24, 2010 You are Nightslyr, the post was more to see if there were any reasons not to pursue that method of type hinting. I've gone ahead and abstracted everything a bit more. There is now a TypeHinting class that you can extend or instantiate separately to give you access to the TypeHinting abstraction. There is also a TypeDef class that gives you access to primitive Type Definitions, but of course, you are free to extend it and implement static versions of your own Type Definitions. <?php /** * Allows you to tie in to the basic TypeHinting methods * to allow pseudo-type hinting within PHP * * @author xangelo */ class TypeHinting { private $structs; /** * * @param string $type Class Property * @param function $cb Callback Method */ protected function AddTypeDef($type,$cb) { $this->structs[$type] = $cb; } /** * * @param string $key Class Property * @param function $value Intented Property Value * @return bool */ protected function IsValidType($key,$value) { if($this->structs[$key] != null) { if(!$this->structs[$key]($value)) { throw new TypeException('Type Exception: '.$key.' type mis-match'); return false; } } return true; } } ?> <?php /** * Creates the default primitive and advanced Type Definitions * for use in Strongly Typing your classes. * * @author xangelo */ class TypeDef { /** * * @param string $x * @return bool */ public static function string($x) { return is_string($x); } /** * * @param int $x * @return bool */ public static function int($x) { return is_numeric($x); } /** * * @param bool $x * @return bool */ public static function bool($x) { return is_bool($x); } /** * * @param double $x * @return double */ public static function double($x) { return is_double($x); } /** * * @param float $x * @return float */ public static function float($x) { return is_float($x); } } /** * Creates base TypeException class * * @author xangelo */ class TypeException extends Exception {} ?> And once again, our account class that implements TypeHinting utilizing our TypeDef helper: <?php /** * Sample Account class to implement type hinting * * @author xangelo */ require_once('TypeDef.php'); require_once('TypeHinting.php'); class Account extends TypeHinting{ private $ID; private $Name; private $Password; public function __construct() { $this->AddTypeDef('ID',function($x){return TypeDef::int($x);}); $this->AddTypeDef('Name',function($x){return TypeDef::string($x);}); $this->AddTypeDef('Password',function($x){return TypeDef::string($x);}); } public function __get($key) { return $this->$key; } public function __set($key,$value) { if(property_exists($this,$key)) { if($this->IsValidType($key,$value)) { $this->$key = $value; } } } } try { $Account = new Account(); $Account->ID = 521; $Account->Name = 'Angelo'; $Account->Password = '49g10fc0q0weq'; echo '<pre>'.print_r($Account,true).'</pre>'; } catch(TypeException $e) { echo $e->getMessage(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222522-thoughts-on-strongly-typed-implementation-for-php/#findComment-1151052 Share on other sites More sharing options...
lastkarrde Posted December 25, 2010 Share Posted December 25, 2010 You could also use SPL types or your own implementation (class with __toString() method). Quote Link to comment https://forums.phpfreaks.com/topic/222522-thoughts-on-strongly-typed-implementation-for-php/#findComment-1151298 Share on other sites More sharing options...
Anti-Moronic Posted December 25, 2010 Share Posted December 25, 2010 Yup, no point in reinventing the wheel. SPL have already done it, among building on many other much needed functions. You should check it out, it's very helpful. Quote Link to comment https://forums.phpfreaks.com/topic/222522-thoughts-on-strongly-typed-implementation-for-php/#findComment-1151302 Share on other sites More sharing options...
johnny86 Posted December 27, 2010 Share Posted December 27, 2010 This is an example from Finnish programming site, but the comments are in English. You should check this out. http://www.ohjelmointiputka.net/koodit_nayta.php?tunnus=1814 Quote Link to comment https://forums.phpfreaks.com/topic/222522-thoughts-on-strongly-typed-implementation-for-php/#findComment-1151926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.