note 86216 added to language.oop5.typehinting

From: Date: Wed, 08 Oct 2008 13:30:08 +0000
Subject: note 86216 added to language.oop5.typehinting
Groups: php.notes 
Request: Send a blank email to php-notes+get-145161@lists.php.net to get a copy of this message
I find it rather frustrating that PHP has internal data types but doesn't allow optional hinting for it. I REALLY needed it for something, so I found a way around it. <?php abstract class DataType { protected $length; protected $precision; protected $number; public function __construct() { $this->number = false; $this->precision = null; } public function GetLength() { return $this->length; } public function IsNumber() { return $this->number; } public function GetPrecision() { return $this->precision; } } class Integer extends DataType { public function __construct($length = 12) { parent::__construct(); $this->number = true; $this->length = $length; $this->precision = 0; } } class Float extends DataType { public function __construct($length = 12, $precision = 2) { parent::__construct(); $this->number = true; $this->length = $length; $this->precision = $precision; } } class String extends DataType { public function __construct($length = 255) { parent::__constructor(); $this->length = $length; } } //etc etc through the types... ?> then later I can do this... <?php final class Field { public $Name; public $Mandatory; public $Hidden; public $ListField; public $Value; public $ForeignClass; public $ReadOnly; public $DataType; public function __construct($name, DataType $dataType, $mandatory = false, $listField = true, $value = null, $readOnly = false, BaseCBO $foreignClass = null) { $this->Name = $name; $this->DataType = $dataType; $this->Mandatory = $mandatory; $this->ListField = $listField; $this->Value = $value; $this->ReadOnly = $readOnly; $this->ForeignClass = $foreignClass; } } // .... class DoSomeStuff { public function DoGenericThings(Field $field) { if ($field->DataType instanceof Integer) { // do things for an integer field... } } } ?> I asked myself a few times if this was the right language for my problem, but there are so many good things about PHP that I made this as a dirty work around. ---- Server IP: 117.104.160.194 Probable Submitter: 203.214.47.162 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.typehinting.php Edit -- https://master.php.net/note/edit/86216 Del: integrated -- https://master.php.net/note/delete/86216/integrated Del: useless -- https://master.php.net/note/delete/86216/useless Del: bad code -- https://master.php.net/note/delete/86216/bad+code Del: spam -- https://master.php.net/note/delete/86216/spam Del: non-english -- https://master.php.net/note/delete/86216/non-english Del: in docs -- https://master.php.net/note/delete/86216/in+docs Del: other reasons-- https://master.php.net/note/delete/86216 Reject -- https://master.php.net/note/reject/86216 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145161) next »