views:

49

answers:

2

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);
+3  A: 

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.

Scott Saunders
+1 for the developer comment.
eykanal
i can understand the developer who inherits this would prob be like wtf, but this would make data type manipulation very easy to do. :/
RobertPitt
also with the toString it should be fully compatible with all php functions
RobertPitt
Think of all the built in PHP functions that work on a string or an array - there are literally hundreds. All of them will be useless to you. PHP will not use your String definition in place of its own internal string system. A string is not an object in PHP, and you cannot extend it the way you can in other OOP languages.
Scott Saunders
Updated with POC for function use.
RobertPitt
I'm learning something new. Works for strings, how about Arrays? Still don't see any advantage though.
Scott Saunders
Well looking into, SPL functions by creating an object that implements ArrayAccess and Itterator it should be fully compatible. the same for New Object(), aslong as it extends stdClass should be ok to use as normal
RobertPitt
My understanding is that ArrayAccess and Iterator are not enough for your class to be used with all of the array functions. Someone was talking about this at a recent PHP meetup, but I don't remember the details. Your idea seems a little more plausible to me now, but I still don't think there's enough benefit to be worth the work. And I still think anyone else who works with your code will not think highly of it :)
Scott Saunders
well if you could understand the ideas in my mind then you would think it was plausible, the Array class would be specifc to teh framework so another another example is `$Array->SendToDebugger('ArrayId')` etc.
RobertPitt
@Scott Almost none of the the array functions are usable with objects and the few that are treat the object as if it were an array where the properties are the elements.
Artefacto
@Artefacto, the array functions would be handled internally, so instead of `array_splice($ArrayObject);` it would be like so `$ArrayObject->Splice(0,12)->implode(',');` if you know what I mean, so it would be ok.
RobertPitt
A: 

Sounds like way too much trouble for... seemingly no benefit.

If you're worried about forgetting to escape stuff, stop escaping things altogether and start using parametrized queries.

Matti Virkkunen
there would be a lot more to the classes such as segment(0,22) and so on its not just going to be db escape, tahts least of my worries, but the principle was taken from the JavaScript engine, and the benefits of being able to manipulate any datatype on the fly i think is good.
RobertPitt