Let’s say I have a database of Objects, Persons and Buildings.
An example of a Person ID is PRS001.
An example of a Building ID is BUL001.
Persons is a table holding all the persons; Buildings is a table holding all the buildings. Object holds all the Persons and all the Buildings.
Now if I extract all data from Object, how do I know if it’s a Person or a Building?
I could do something like this:
if(start_of_string_is(BUL))
then it's a building
else if(start_of_string_is(PRS))
then it's a person
But is there not a more succinct and sexier way of achieving this outcome?
You are making a fundamental design flaw here. A
personis not abuilding, and abuildingis not aperson.In the object orientent world, those would be different type of objects, and to check a instance of a object against its type you would use InstanceOf.
Furthermore, you are using the key to get information about the record. That is not how keys suppose to work. Keys uniquely identify a record, without having a meaning.
To make things worse, you already have 2 tables. So why not instantiate a person object from the persons table, and a building object from the buildings table. PDO can instantiate those objects automatic on fetching.
Example:
when fetching a row from the persons table.