Example In Changes in PHP 5/Zend Engine 2.0

From: Chong Wee Huat (chongwh_at_yahoo.com)
Date: 11/16/03


To: php-general@lists.php.net
Date: Sun, 16 Nov 2003 07:50:22 +0800

Below is the first php example found in "Changes in PHP 5/Zend Engine
2.0 "

<?php
class MyClass {
    private $Hello = "Hello, World!\n";
    protected $Bar = "Hello, Foo!\n";
    protected $Foo = "Hello, Bar!\n";

    function printHello() {
        print "MyClass::printHello() " . $this->Hello;
        print "MyClass::printHello() " . $this->Bar;
        print "MyClass::printHello() " . $this->Foo;
    }
}

class MyClass2 extends MyClass {
    protected $Foo;

    function printHello() {
        MyClass::printHello(); /* Should print
*/
        print "MyClass2::printHello() " . $this->Hello; /* Shouldn't
print out anything */
        print "MyClass2::printHello() " . $this->Bar; /* Shouldn't
print (not declared)*/
        print "MyClass2::printHello() " . $this->Foo; /* Should print
*/
    }
}

$obj = new MyClass();
print $obj->Hello; /* Shouldn't print out anything */
print $obj->Bar; /* Shouldn't print out anything */
print $obj->Foo; /* Shouldn't print out anything */
$obj->printHello(); /* Should print */
?>

The result not same as stated in the remark. No print for
$obj->printHello().

It only print with the code below

$obj = new MyClass();
//print $obj->Hello; /* Shouldn't print out anything */
//print $obj->Bar; /* Shouldn't print out anything */
//print $obj->Foo; /* Shouldn't print out anything */
$obj->printHello(); /* Should print */

Accessing protected variable cause the output fail, why?



Relevant Pages

  • Re: CArray
    ... MyClass * obj; ... Error 2664 Add cannot convert parameter 1 from class MyClass to class ... Personally, also, I would forget about CArray. ...
    (microsoft.public.vc.language)
  • Re: IDE for PHP
    ... Gary L. Burnore wrote: ... MyClass $obj = new MyClass; ... Since PHP is loosely typed, the "MyClass" preceding $obj is bad syntax. ...
    (comp.lang.php)
  • Re: IDE for PHP
    ... MyClass $obj = new MyClass; ... Hmmm...how could that be done in PHP, ... If you like visual studio, i suggest getting the VS.php plugin, its ...
    (comp.lang.php)
  • Re: Creating Classes at runtime
    ... and you also want to refer to it via the bareword constant MyClass then you ... class MyClass; end ... obj = MyClass.new ... we can never obtain peace in the outer world until we make peace with ...
    (comp.lang.ruby)
  • CArray
    ... CArray arr; ... MyClass * obj; ... Error 2664 Add cannot convert parameter 1 from class MyClass to class ...
    (microsoft.public.vc.language)