MasterMastic
MasterMastic

Reputation: 21306

SQL Class Data Type

Is it possible to make the data type of a column in a table an object of a class?

For instance, if I have a class that manages strings called "MyString", is it possible to use it instead of varchar?

Upvotes: 1

Views: 145

Answers (3)

aF.
aF.

Reputation: 66747

If you weren't using MYSQL probably you could use User-Defined Datatypes.

But since they are not available for MYSQL, you only can use this Datatypes.

Upvotes: 0

Justin T.
Justin T.

Reputation: 3701

No, you cannot. That's why we have ORM : Object Relational Mapping

Quoting Wikipedia :

Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database"

I think that's what you were looking for, good luck !

For good ORMs libraries in PHP, look at this : Good PHP ORM Library? on S.O

Upvotes: 3

Mark Baker
Mark Baker

Reputation: 212522

No, databases are separate entities to scripting languages, so a database like MySQL has no concept of a PHP Class.... and PHP Classes are different to (for example) Java classes, so to implement this, every database would need a datatype for every conceivable class-based language.... and then consider the practicalities of a PHP script wanting to access data stored by a Java app.

What you can do, however, is serialize objects, and store them in a database table column, of type VARCHAR... but beware that private/protected methods serialise with NULL characters in the serialized string

Upvotes: 0

Related Questions