PHP类的用法

<?php
$names = fix_names("WILLIAM", "henry", "gatES");
echo $names[0] . " " . $names[1] . " " . $names[2];

function fix_names($n1, $n2, $n3)
{
    $n1 = ucfirst(strtolower($n1));
    $n2 = ucfirst(strtolower($n2));
    $n3 = ucfirst(strtolower($n3));
    return array($n1, $n2, $n3);
}
?>

<?php
$fp = fopen("text.txt", 'wb');

for ($j = 0 ; $j < 100 ; ++$j)
{
    $written = fwrite($fp, "data");
    if ($written == FALSE) break;
}

fclose($fp);
?>


<?php
$object = new User;
print_r($object);

class User
{
    public $name, $password;

    function save_user()
    {
        echo "Save User code goes here";
    }
}
?>

<?php
User::pwd_string();

class User
{
    static function pwd_string()
    {
        echo "Please enter your password";
    }
}
?>
<?php
$object1 = new User();
$object1->name = "Alice";
echo $object1->name;

class User {}
?>

<?php
class Test
{
    public $name     = "Paul Smith"; // Valid
    public $age      = 42;           // Valid
    public $time     = time();       // Invalid - calls a function
    public $score    = $level * 2;   // Invalid - uses an expression
}
?>

<?php
$temp = new Test();
echo "Test A: " . Test::$static_property . "<br />";
echo "Test B: " . $temp->get_sp()        . "<br />";
echo "Test C: " . $temp->static_property . "<br />";
class Test
{
    static $static_property = "I'm static";
    function get_sp()
    {
        return self::$static_property;
    }
}
?>

<?php
$object           = new Subscriber;
$object->name     = "Fred";
$object->password = "pword";
$object->phone    = "012 345 6789";
$object->email    = "fred@bloggs.com";
$object->display();

class User
{
    public $name, $password;

    function save_user()
    {
        echo "Save User code goes here";
    }
}

class Subscriber extends User
{
    public $phone, $email;

    function display()
    {
        echo "Name:  " . $this->name     . "<br />";
        echo "Pass:  " . $this->password . "<br />";
        echo "Phone: " . $this->phone    . "<br />";
        echo "Email: " . $this->email;
    }
}
?>

<?php
$object = new Son;
$object->test();
$object->test2();

class Dad
{
    function test()
    {
        echo "[Class Dad] I am your Father<br />";
    }
}

class Son extends Dad
{
    function test() {
        echo "[Class Son] I am Luke<br />";
    }    
    function test2() {
        parent::test();
    }
}
?>

<?php
$object = new Tiger();
echo "Tigers have...<br>";
echo "Fur: " . $object->fur . "<br />";
echo "Stripes: " . $object->stripes;

class Wildcat
{
    public $fur; // Wildcats have fur

    function __construct()
    {
        $this->fur = "TRUE";
    }
}

class Tiger extends Wildcat
{
    public $stripes; // Tigers have stripes

    function __construct()
    {
        parent::__construct(); // Call parent constructor first
        $this->stripes = "TRUE";
    }
}
?>
原文地址:https://www.cnblogs.com/findumars/p/2909479.html