zoukankan      html  css  js  c++  java
  • 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";
        }
    }
    ?>
  • 相关阅读:
    拷贝某文件至某位置
    Java对象的序列化和反序列
    常见的RuntimeException异常有哪些
    array数组增删元素
    失眠怎么办
    构造函数和函数区别(关键的new操作符)
    匿名函数递归(arguments.callee)和命名函数递归
    localeCompare方法在chrome浏览器取值问题
    random()方法
    iframe 父子页面之间取值
  • 原文地址:https://www.cnblogs.com/findumars/p/2909479.html
Copyright © 2011-2022 走看看