Beispiel #1
0
function main()
{
    $k = new Foo("something");
    echo $k->getX();
    echo "\n";
    $k->setX("foo");
    echo $k->getX();
    echo "\n";
    $k->setXVerified("string");
    echo $k->getX();
    echo "\n";
    $k->setY(new Bar());
}
Beispiel #2
0
</head>
<body>
<?php 
//  Some simple OOP in PHP.
if (isset($theFoo)) {
    echo "Object ", $theFoo, " has been restored <br/>";
} else {
    // Creating a new object looks a lot like Java
    $fooVar = new Foo(77, 88, 99);
    // When using the object in a String context, the "magic" toString()
    // method will implicitly be called.  See Foo.php.
    echo "fooVar is ", $fooVar, "<br/>";
    // Unlike Java (but like C++) we use the arrow notation rather than the
    // "dot" notation to access methods and data within an object
    $fooVar->setX(70);
    $fooVar->setY(80);
    // Since z is public, we can access it directly if we wish
    $fooVar->z = 90;
    echo "fooVar is ", $fooVar, "<br/>";
    $subFooVar = new SubFoo(22, 33, 44, 55);
    echo "subFooVar is ", $subFooVar, "<br/>";
    // We can access setX and setY via inheritance
    $subFooVar->setX(20);
    $subFooVar->setY(30);
    $subFooVar->z = 40;
    $subFooVar->setSub(50);
    echo "subFooVar is ", $subFooVar, "<br/>";
    echo "x field is " . $subFooVar->subGetX() . "<br/>";
    echo "y field is " . $subFooVar->subGetY() . "<br/>";
    echo "<br/>";
    // "Kind of" polymorphic access.  Note that we are mixing a subclass