Page 1 of 2

Class named Object

Posted: Fri Jun 29, 2007 7:15 am
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

Posted: Fri Jun 29, 2007 7:22 am
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.

Re: Class named Object

Posted: Fri Jun 29, 2007 10:31 am
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.

Posted: Fri Jun 29, 2007 10:38 am
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 ?

Posted: Fri Jun 29, 2007 10:42 am
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.

Re: Class named Object

Posted: Fri Jun 29, 2007 11:39 am
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.

Re: Class named Object

Posted: Fri Jun 29, 2007 11:53 am
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.

Posted: Fri Jun 29, 2007 12:40 pm
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.

Posted: Fri Jun 29, 2007 12:56 pm
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.

Posted: Fri Jun 29, 2007 1:03 pm
by superdezign
But what "generic behaviors" would even be shared by *all* of your classes?

Posted: Fri Jun 29, 2007 1:08 pm
by anjanesh
superdezign wrote:But what "generic behaviors" would even be shared by *all* of your classes?
Class Object

Posted: Fri Jun 29, 2007 1:22 pm
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.

Posted: Fri Jun 29, 2007 1:44 pm
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.

Posted: Fri Jun 29, 2007 1:55 pm
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.

Posted: Fri Jun 29, 2007 2:39 pm
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.