Starting OOP and I followed a tutorial.. how can i get this to print "hello"
PHP Code:<?php
class words {
function writeText(){
echo "hello";
}
}
$words = new $words;
$words->writeText;
?>
| SitePoint Sponsor |




Starting OOP and I followed a tutorial.. how can i get this to print "hello"
PHP Code:<?php
class words {
function writeText(){
echo "hello";
}
}
$words = new $words;
$words->writeText;
?>




$words = new $words;
should be
$words = new words;
As BerislavLopac mentioned
and remember to use parenthesis on the method call:PHP Code:$instance = new words;
PHP Code:$instance->writeText();
Jason Sweat ZCE - jsweat_php@yahoo.com
Book: PHP Patterns
Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
Detestable (adjective): software that isn't testable.




$words->writeText();
TheDrunkenEpic - my ramblings




Big thanks guys!
What do you use to write your code? Dreamweaver? Notepad ++? PHP designer?? Eclipse?
Notepad++ for me.
Small hint though. Unless the whole point of the object is to output text to the browser, don't use echo within a class.
A prefered method would be:
PHP Code:<?php
class Words{
function getText(){
return 'hello';
}
}
$words = new Words;
$words->getText();
?>
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona




thanks ark, input appreciated. Just trying to write my first class and was testing. Thx
EditPlus3
TheDrunkenEpic - my ramblings



Personally I use Intype for small scripts (excellent text editor) and an IDE for building really complex applications (such as frameworks or CMS). The best IDE for PHP is probably Zend Studio. Pretty cheap too. NuSphere PhpEd has some nice integration with markup languages though.
PHPDesigner is weak. Dreamweaver's PHP editor is a joke (well maybe not a joke, but can't be taken seriously in comparison to the others) and PHPEclipse just simply annoys me.
NuSphere PhpEd has been loyal to me although if I had to pick one from the ones available now for the end of my life I'd pick Zend Studio. They've done it all right there, editor wise. It's just... I dunno. Not very inviting, I guess. But it's up to you.
Learn about the new Retro Framework
Code PHP the way it was meant to be coded!



For one, it barely supports variables/function documention.
For example in a class I'd do
/**
* @var myObject
*/
public $myObject;
to let most good IDEs realize that that's the type of this variable, meaning... code insight.
Not to mention it's extremely annoying. It doesn't wait a second since you type the class keyword and the final '}' before it starts spitting out red lines and supposed errors. You think it'd give you at least 3 seconds before shouting at you with these red flashy and distracting colors. But I guess it has other plans. I always feel like it's mocking me.![]()
Learn about the new Retro Framework
Code PHP the way it was meant to be coded!


Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!



Haha.I've only used the (paid) version of 2007 though. Not sure about 2008's documentation support.
Learn about the new Retro Framework
Code PHP the way it was meant to be coded!

Eclipse PDT for me. I love its debugger. Helps me out a lot when I make the common typo.![]()


Lets not let this thread get into another "I use this - You use that" IDE thread! We have plenty of them already!
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

The seed has been planted.
Anyway, back on topic. When using OOP remember to actually consider your class as an object with actual properties. Try to avoid creating classes that really do nothing and are simply wrappers for nothing special. Then you're creating classes just for the sake of it and it makes your code a lot less logical. A class needs to be an object, like in the real world, the methods manipulate its properties, and the object contains useful info that can be passed around your app. Just some Lami odd theory for you there.![]()



I have to disagree. I think using classes as static function libraries can be very very helpful and convenient at times. It doesn't really use up more resources, too, nor makes the file size bigger by more than a few bytes.
global functions FTL, is what I'm trying to say.![]()
Learn about the new Retro Framework
Code PHP the way it was meant to be coded!



No, no. Not objects, classes.
As in to have all the string functions under one roof is very useful, since there's no use in creating a string object.
etc etc. Very convenient.PHP Code:Class String {
public static function upper($str) {
return strtoupper($str);
}
}
Learn about the new Retro Framework
Code PHP the way it was meant to be coded!



PHP is known to suck at function names. It's all cluttered up.
I find this way to be very helpful for keeping my sanity at a decent level.
And if that seems a bit pointless to you then well.. Hehe. I don't know what to say. What's more important for modern coding than organization and readability?
Learn about the new Retro Framework
Code PHP the way it was meant to be coded!

I think people make too much of a fuss about the function names. Yes, they are a bit of a mess, but I've never ever found it to be a problem... you just learn what they are, same as any language. Autocomplete in your IDE is also very useful
I was referring more to use a static class instead of just using functions though. What's wrong with defining all the functions in another file, functions.string.php or whatever? Using a class for it seems very overkill.
Bookmarks