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;
?>Moderator: General Moderators
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;
?>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();
?>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;
?>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.