1

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?

JJJ
  • 32,902
  • 20
  • 89
  • 102
MasterMastic
  • 20,711
  • 12
  • 68
  • 90
  • uhm...not really clear...you want a "PHPOBJECT" column in MySql??? – Damien Pirsy Jan 03 '12 at 09:30
  • You cant create PHP objects in mysql but there are some DB servers available where we can change column type to database object. search for object oriented database for more info – Uday Sawant Jan 03 '12 at 09:37

3 Answers3

3

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

Community
  • 1
  • 1
Justin T.
  • 3,643
  • 1
  • 22
  • 43
0

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

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

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.

aF.
  • 64,980
  • 43
  • 135
  • 198