Page 1 of 1
A dummy class
Posted: Fri Apr 04, 2008 7:50 pm
by ngungo
What is wrong with this code? It does not output anything. Frustrated.
Code: Select all
<?php
class dummy {
function dummy($one, $two) {
$val = $one + $two;
return $val;
}
}
$i = 1;
$j = 2;
$x = new dummy($i, $j);
echo 'x = ' .$x;
?>
Re: A dummy class
Posted: Fri Apr 04, 2008 8:14 pm
by EverLearning
Constructors don't return values.
Read a little about classes -
PHP Manual
Re: A dummy class
Posted: Fri Apr 04, 2008 8:25 pm
by ngungo
Thanks!
Re: A dummy class
Posted: Fri Apr 04, 2008 8:28 pm
by Christopher
Code: Select all
<?php
class dummy {
var $val = 0;
function dummy($one, $two) {
$this->val = $one + $two;
}
function show() {
return $this->val;
}
}
$x = new dummy(1, 2);
echo 'x = ' . $x->show();
?>
In PHP5
Code: Select all
<?php
class dummy {
protected $val = 0;
function __construct($one, $two) {
$this->val = $one + $two;
}
function __toString() {
return $this->val;
}
}
$x = new dummy(1, 2);
echo 'x = ' . $x;
?>
Re: A dummy class
Posted: Fri Apr 04, 2008 9:51 pm
by ngungo
I understand now. Thanks!
Thing is this
article gave me misinformation:
See how you can pass arguments right inside of your "new dummy3()" call? That's what's special about this. Though it might seem rather pointless, this is great to use for functions you use repetitively and want to make calls to with fewer lines of code. "$sum" will now return 3 if you did "echo $sum" by the way.
Re: A dummy class
Posted: Sat Apr 05, 2008 5:06 am
by Chris Corbyn
That's bad. Mr Spoono is naughty! And I used to recommend spoono as a beginner's guide to OOP. I won't now
