Class named Object

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Class named Object

Post by anjanesh »

Many OOP languages have a base class called Object.

I want to create a base class called Object from which I want to derive all other classes.
Will creating a class named Object be an issue for future versions of php ? [ Remember nuSoap's soapclient conflict with PHP5's SoapClient ]
Is PHP6 implementing this kind of OOP structure ?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Naming it something other than strictly "object" would prevent any mismatches. For example all my classes are prefixed with "DNA" in the DNA framework. Sure, it's some extra typing, but it also avoids naming conflicts with third party code that someone may use.
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Re: Class named Object

Post by ev0l »

anjanesh wrote:Many OOP languages have a base class called Object.

I want to create a base class called Object from which I want to derive all other classes.
Will creating a class named Object be an issue for future versions of php ? [ Remember nuSoap's soapclient conflict with PHP5's SoapClient ]
Is PHP6 implementing this kind of OOP structure ?
I don't think it would be a bad idea. Changing it in the future would not be that hard. It simply a matter of personal taste.

I would highly recommend , no matter what you name it, that you use a base class.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Changing it in the future would not be that hard
If I use this base class in say 10 websites and all other classes that descend directly from Object will have extend Object and static methods like Object::foo() will exist.

True, as feyd said avoids naming conflicts with third party code that someone may use.

but still...want to know if php6 is going to be totally Object Oriented like Java and .NET where a base class called Object will exist as a base ?
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Post by ev0l »

anjanesh wrote:
Changing it in the future would not be that hard
If I use this base class in say 10 websites and all other classes that descend directly from Object will have extend Object and static methods like Object::foo() will exist.

True, as feyd said avoids naming conflicts with third party code that someone may use.

but still...want to know if php6 is going to be totally Object Oriented like Java and .NET where a base class called Object will exist as a base ?
The convention I use is my initials prefacing all object names. Oddly though I don't do this with my base class.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Class named Object

Post by Christopher »

I would recommend against using a base class in PHP. I don't consider it to be a very good practice. Other languages tuck "universal" capabilites into all of their objects because everything has to be an object. PHP has no such constraint and helper functions are the practical solution in PHP (like it or not) and they can be lazy loaded. I do agree on the class naming prefix suggestions.
(#10850)
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Re: Class named Object

Post by ev0l »

arborint wrote:I would recommend against using a base class in PHP. I don't consider it to be a very good practice.
Wow! I have never heard anyone give that recommendation before. One of the advantages of OOP is inheritance and using it from the beginning gives you capabilities of dealing with objects in generic ways that you would not of had with out it.

If you are going to have most of your objects inherit from nothing you might as well use functions.

Even if your base object contains nothing in the beginning you should have one because, trust me, before the project is completed your base class with have defined behavior.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

By base class I did not mean to never inherit -- I meant to have a single class that every class inherits. That is what the OP asked about. I consider that a poor practice in PHP.
(#10850)
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Post by ev0l »

arborint wrote:By base class I did not mean to never inherit -- I meant to have a single class that every class inherits. That is what the OP asked about. I consider that a poor practice in PHP.
I mean a base class. If you are creating a new class that does not obviously inherit from anything it should inherit from a class that defines a generic set of behaviors. Unless, of course, you are creating the class that defines those behaviors or special circumstances like proxy objects. It is almost always wise to have all your objects in a single project stem from a single object, that is having a base object that is referenced as the root object in your inheritance tree.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

But what "generic behaviors" would even be shared by *all* of your classes?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

superdezign wrote:But what "generic behaviors" would even be shared by *all* of your classes?
Class Object
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Post by ev0l »

superdezign wrote:But what "generic behaviors" would even be shared by *all* of your classes?
Error handling, testing (isThingy() methods), certain types of reflection, overwriting __toString, creating unique object identifiers, Objective-C style key paths....

Code: Select all

public function getByKeyPath($aStringKeyPath){
			return $this->$aStringKeyPath();
		}
		public function setByKeyPath($aStringKeyPath,$value){
			$methodName = "set".ucfirst($aStringKeyPath);
			$this->$methodName($value);
		}
Testing is one of the most useful reasons to do this. Say for example you are writing a generic way of storing items into a database. Most object will just respond to __toString with the value that needs to be stored but some items might need the database connect before they are stored. These items respond to needsDbConnection() with true, while every other object in your application will respond with false.


Check out http://www.oldenbuettel.de/squeak-doku/ ... bject.html for a bunch of ideas.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

This and the examples give by ev01 are exactly the reasons why I think it is a poor practice in PHP. Java does it that way because of the constraints of the language. PHP has a different design. If you want to code PHP exactly like Java then just use Java -- it is an awesome language. e01 would add __toString() capablity to all classes when only a very few actually need that functionality. Testing should definitely be external in my opinion.
(#10850)
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Post by ev0l »

arborint wrote:
This and the examples give by ev01 are exactly the reasons why I think it is a poor practice in PHP. Java does it that way because of the constraints of the language. PHP has a different design. If you want to code PHP exactly like Java then just use Java -- it is an awesome language. e01 would add __toString() capablity to all classes when only a very few actually need that functionality. Testing should definitely be external in my opinion.
Actauly as of PHP 5.2 all classes have __toString functionality built in. I just don't care for the format. For error reporting purposes all classes need __toString.

Making type testing external causes tightly coupled code and hurts maintainability and reusability. The object should encapsulate all it's behavior including testing (as much as the language allows).

Using a base object at a bare minimum can't hurt and in fact is hugely beneficial.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

ev0l wrote:Using a base object at a bare minimum can't hurt and in fact is hugely beneficial.
If you use methods like that a lot then it may be of benefit to you. I prefer not to implement anything unless I really need it -- and then only where I need it. For me, convenience is the enemy of good design in the long run.
(#10850)
Post Reply