Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Data-types classes

Im thinking of making a custom datatypes / prototypes for a project im working on but im wondering if its such a good idea?

For example

class String
{
    var $Value;
    private $escaped = false;

    function __construct($String)
    {
        $this->Value = $String;
    }

    function escape()
    {
        if($escaped === false)
        {
            $this->Value = Registry::get('Database')->escape($this->Value);
        }
        return $this;
    }

    function trim()
    {
        $this->Value = trim($this->Value);
        return $this;
    }

    function __toString()
    {
        return $this->__toString();
    }
}
$myValue = new String('Hello World')->trim()->escape();
//$myValue is now prepared for DB insert

There will be prototypes for Array, Object, String, Resource etc..

with arrays there will implement Iterator and such

Some benefits i have in mind is specific data types to objects for example

interface Insert
{
    public function Insert(String $Value); //Array / Object / Resource
}

The custom prototypes would be useful for all strings.

But do you think that the amount of resource usage will out way the benefits ?


updated for POC

$String = new String('ValueText');

sprintf('Test %s',$String); //Works

trim($String); //Works

base64_encode($String); //Works

Also for arrays the SPL Library would be perfect.

class Array implements ArrayAccess, Iterator, Countable
{
   public function __construct(){}
   public function offsetSet($offset,$value){}
   public function offsetExists($offset){}
   public function offsetUnset($offset){}
   public function offsetGet($offset){}
   public function rewind(){}
   public function current(){}
   public function key(){}
   public function next(){}
   public function valid(){}
   public function count(){}
}

Another idea would be the extendible entities

class DatabaseVariable extends String
{
    function __construct($string)
    {
        parent::__constrcut($string);
    }

    public function escape()
    {
        //Blah
    }
}

Having a new entity extend a data-type will make it inherit available methods for that data-type.

As discussed about autoboxing, this is the exact system im looking for but as its not passed discussions yet, for my new project (Forum System) witch I started the other day, do you think that I should go ahead and use my idea?, the user will be able to do faster interactions with datatypes, and if there is a function that does not support an object being passed, we can also do

$RawResource = $Resourtce->Raw();

//...

$Resource->Set($RawResource);
like image 946
RobertPitt Avatar asked Dec 01 '22 05:12

RobertPitt


1 Answers

In my opinion, the time you spend writing this code, fixing this code, and cursing the fact that you can't use hundreds of PHP functions with your classes will outweigh any advantage this code may have.

Also, the developer who inherits your project will hate you.

like image 92
Scott Saunders Avatar answered Dec 22 '22 04:12

Scott Saunders