[PHP] Re: Basics of OOP

2008-09-09 Thread Nathan Rixham

Ben Stones wrote:

Hi,

The following bit of code doesn't output anything:

?php
class output {
var $text;
function outputText() {
$this-text = Hello World;
echo $this-text;
}
}

$class = new output();
$class-outputText;
?

As well as the following code:

?php
class output {
var $text=5;
function outputText() {
echo $this-text;
}
}

$class = new output();
$class-outputText;
?

Am I doing anything wrong? By the way, the preceding code is just so I
understand OOP which finally I've grasped, but am I doing anything wrong as
both codes don't output anything?



it's the lack of a methodCall()

$class-outputText; don't work
$class-outputText(); will work

while you're there why not learn the php5 way?

?php
class output {

  private $text;

  public function outputText( $text='default text' )
  {
$this-text = $text;
return $this-text;
  }

}

$class = new output;
$class-outputText();

?

just a suggestion save you double learning! good choice on the OO though

nathan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Basics of OOP

2008-09-09 Thread Jochem Maas

Nathan Rixham schreef:

Ben Stones wrote:

Hi,

The following bit of code doesn't output anything:

?php
class output {
var $text;
function outputText() {
$this-text = Hello World;
echo $this-text;
}
}

$class = new output();
$class-outputText;
?

As well as the following code:

?php
class output {
var $text=5;
function outputText() {
echo $this-text;
}
}

$class = new output();
$class-outputText;
?

Am I doing anything wrong? By the way, the preceding code is just so I
understand OOP which finally I've grasped, but am I doing anything 
wrong as

both codes don't output anything?



it's the lack of a methodCall()

$class-outputText; don't work


$class-outputText;

references an undefined property of the class instance,
turning on error_reporting to full (i.e. including E_NOTICE)
would have given a hint to the OP (assuming he has display_error ON
or he is reading his error log) as to what was going on.

as an example, try running this:


?

class Foo {
public  $a = A\n;
private $b = B\n;
static  $d = D\n;

function b() { echo $this-b; }
static function d() { echo self::$d; }
static function e() { echo E\n; }
}


ini_set('display_errors', true);
ini_set('error_reporting', E_ALL ^ E_STRICT);

$f = new Foo;
echo $f-a;
echo $f-b();
echo $f-c;
echo $f-d();
echo $f-d;
echo Foo::d();
echo Foo::$d;
echo $f-e();
echo Foo::e();

// the following are fatal errors:

echo $f-b;
echo Foo::b();

?


$class-outputText(); will work

while you're there why not learn the php5 way?


quite right :)


?php
class output {

  private $text;

  public function outputText( $text='default text' )
  {
$this-text = $text;
return $this-text;
  }

}

$class = new output;
$class-outputText();

?

just a suggestion save you double learning! good choice on the OO though

nathan




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php