homepage Welcome to WebmasterWorld Guest from 54.243.17.133
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Object-Oriented 'Best Practice' Question
instantiating objects
adephue




msg:4192725
 7:34 pm on Aug 26, 2010 (gmt 0)

I have a question about how PHP classes relate to each other. Specifically, I am hoping to discern if a certain practice is unfavorable. In my example I have two classes, 'foo' and 'bar'.

class bar {
function talk () {$this->talk = 'Hello!';}
}

class foo {
function foo () {
bar::talk();
self::talk();
}
function talk () {echo $this->talk;}
}

$foo = new foo(); // This will echo 'Hello!' to the screen
echo '<p><pre>';
var_dump($foo);


If you run the posted code, the property 'talk' is assigned to object 'foo' even though the code is in object 'bar'. Or stating it another way, by calling bar::talk(); from within the context of object 'foo', I am able to utilize the functionality of object 'bar' within the context of object 'foo'. I never instantiate 'bar' with the new keyword.

So my question is whether or not this is a bad or non-standard OOP practice. I am doing some development this way but before I go much further I want to know if there is some big no-no or caveat that I am missing.

 

httpwebwitch




msg:4194313
 7:15 am on Aug 31, 2010 (gmt 0)

nothing wrong with what you're doing. but you might want to EXTEND the class instead of calling functions from it.

if foo is intended to inherit the properties and methods of bar, then foo should extend bar.

the classic example: the dog that is an animal

class DOG extends ANIMAL {
var $age;

function talk() {
echo "bark! bark!";
}
}

calling bar::talk() from within foo is fine, but if your class structures get more complex than this then it could get confusing

CyBerAliEn




msg:4195807
 10:04 pm on Sep 2, 2010 (gmt 0)

class DOG extends ANIMAL {
var $age;

function talk() {
echo "bark! bark!";
}
}


This is my favorite example of coding, ever! I got a good laugh from reading this. :)

httpwebwitch




msg:4196090
 2:47 pm on Sep 3, 2010 (gmt 0)

Most thorough books on PHP5 will have a chapter on OOP. It's worthwhile to become familiar with inheritance even if you don't delve deeply into advanced OOP. Every time I learn a new OOP technique (and I am still learning), it makes everything simpler, and I wish I'd studied OOP more thoroughly, earlier!

One guide I turn to often is the Visual QuickPro Guide to PHP 5 by Larry Ullman, Peachpit Press 2007

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved