PHP variables as objects

I'm not sure this is a question that's directly answerable but I thought I'd give it a try.

In some other languages variables are created as objects, allowing you to manipulate the value of the variable.

For example, in Objective-C (i believe) you can create a string like this (or similar);

my_name = [NSString]

Open in new window


Does anyone know if anyone has built a similar set of objects for PHP?

For example, I'd envisage it working as such;

$my_name = new pString()->set('John Doe');
$my_dob = new pDateTime()->set('07-08-1980');

echo $my_dob->get('Y-m-d'); // Notice how the get method can handle the formatting for you.

Open in new window


My main reason for asking is I use objects a lot in projects, and think being able to do something like the following would be useful (most of my objects are linked to database tables);

class car {

public $id;
public $make;
public $model;
public $registered_date;

public function __construct() {
$this->id = new pInt()->setLength(11)->autoIncrement(true)->primaryKey(true);
$this->make = new pString()->setLength(20);
$this->model = new pString()->setLength(15);
}

}

Open in new window


A simple object such as pString can then manipulate the string to 1) make sure it's a string, 2) strip it's length to match the length set when the pString object was created and so on. Because we know the variable is a pString object, it would be possible to have the object generate the SQL code required to create the car object in a mysql database, as it would know the type, length of the variable.

I've been thinking of creating something like this for my own projects, however I can't believe that this hasn't already been done and there's a collection of objects that can be added to your project to do just this already?

Does anyone know if such a library of objects exists?
SheppardDigitalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You might find something similar and reasonably useful in Laravel.  It's a framework that wraps many of the PHP functions in its own wrappers, making it easier to create mock objects for testing.  You might also scan GitHub for PHP object-oriented projects, however I've never seen anything quite like this.  For the most part, PHP's loose data typing and rich function set (over 1,500 and counting) has been a substitute for the kind of structure you would expect to find in a modern language (PHP is over 20 years old and was built to be "easy for novices").  We have nothing at all like Ruby on Rails today.  But for the good news, it seems like most of the PHP-World conversations are about simplification, interoperability, standards, etc.  So there may be a brighter future.

For now, just keep a window open to PHP,net whenever you're writing code.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
I've built something similar in the past for a client, so it can be done. The likeliest place where you could find a class like that would probably be in phpclasses.org, but there's no guarantee the end result will be exactly what you want. You might find 5 classes that do a lot of similar things but have different approaches, some which might suit you and some that might not.

For what it's worth, I'd stay away from code that tries to generate table creation / maintenance SQL. That is not the job of code, and it will never do it AS effectively as normal DBA tools (e.g. HeidiSQL for MySQL), and it runs the risk of becoming obsolete syntax over time. It's better for data-bound objects to focus on generating the proper queries for CRUD operations (and, if possible, using a database abstraction library as a middle layer so your code becomes more portable).

Making every value into an object is probably going to give you problems in the long run. it's like every person in a family having their own separate house inside of one big house. It's overkill.

One object can easily represent a record and can contain a definition of various columns in the database, and use the __set and __get methods to process the formatting and modification of column values.
Ray PaseurCommented:
May not be directly applicable but I came across this today.
https://mwop.net/blog/2014-08-11-testing-output-generating-code.html
SheppardDigitalAuthor Commented:
Both responses were both very helpful, thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.