note 86216 deleted from language.oop5.typehinting by nikic
| From: | nikic@php.net | Date: | Sun, 01 Nov 2020 21:51:21 +0000 |
| Subject: | note 86216 deleted from language.oop5.typehinting by nikic | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215691@lists.php.net to get a copy of this message | ||
Note Submitter: info at thewebsiteguy dot com dot au
----
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...
}
}
}
?>