zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然PHP-MySQL-JavaScript学习笔记:PHP函数与对象

    <?php
      $a1 = "WILLIAM";
      $a2 = "henry";
      $a3 = "gatES";
    
      echo $a1 . " " . $a2 . " " . $a3 . "<br>";
      fix_names();
      echo $a1 . " " . $a2 . " " . $a3;
    
      function fix_names()
      {
        global $a1; $a1 = ucfirst(strtolower($a1));
        global $a2; $a2 = ucfirst(strtolower($a2));
        global $a3; $a3 = ucfirst(strtolower($a3));
      }
    ?>
    <?php
      include("library.php");
    
      // Your code goes here
    ?>
    <?php
      include_once("library.php");
    
      // Your code goes here
    ?>
    <?php
      require_once("library.php");
    
      // Your code goes here
    ?>
    <?php
      if (function_exists("array_combine"))
      {
        echo "Function exists";
      }
      else
      {
        echo "Function does not exist - better write our own";
      }
    ?>
    <?php
      $object = new User;
      print_r($object);
    
      class User
      {
        public $name, $password;
    
        function save_user()
        {
            echo "Save User code goes here";
        }
      }
    ?>
    <?php
      $object = new User;
      print_r($object); echo "<br>";
    
      $object->name = "Joe";
      $object->password = "mypass";
      print_r($object); echo "<br>";
    
      $object->save_user();
    
      class User
      {
        public $name, $password;
    
        function save_user()
        {
            echo "Save User code goes here";
        }
      }
    ?>
    <?php
      $object1 = new User();
      $object1->name = "Alice";
      $object2 = $object1;
      $object2->name = "Amy";
    
      echo "object1 name = " . $object1->name . "<br>";
      echo "object2 name = " . $object2->name;
      
      class User
      {
        public $name;
      }
    ?>
    <?php
      $object1 = new User();
      $object1->name = "Alice";
      $object2 = clone $object1;
      $object2->name = "Amy";
    
      echo "object1 name = " . $object1->name . "<br>";
      echo "object2 name = " . $object2->name;
      
      class User
      {
        public $name;
      }
    ?>
    <?php
      class User
      {
        function User($param1, $param2)
        {
          // Constructor statements go here
        }
      }
    ?>
    <?php
      class User
      {
        function __construct($param1, $param2)
        {
          // Constructor statements go here
        }
      }
    ?>
    <?php
      class User
      {
        function __destruct()
        {
          // Destructor code goes here
        }
      }
    ?>
    <?php
      class User
      {
        public $name, $password;
    
        function get_password()
        {
          return $this->password;
        }
      }
    ?>
    <?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
      Translate::lookup();
      
      class Translate
      {
        const ENGLISH = 0;
        const SPANISH = 1;
        const FRENCH  = 2;
        const GERMAN  = 3;
        // ?
        static function lookup()
        {
          echo self::SPANISH;
        }
      }
    ?>
    <?php
      class Example
      {
        var $name = "Michael";   // Same as public but deprecated
        public $age = 23;        // Public property
        protected $usercount;    // Protected property
      
        private function admin() // Private method
        {
          // Admin code goes here
        }
      }
    ?>
    <?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";
        }
      }
    ?>
    <?php
      class User
      {
        final function copyright()
        {
          echo "This class was written by Joe Smith";
        }
      }
    ?>
    <?php
      echo strrev(" .dlrow olleH"); // Reverse string
      echo str_repeat("Hip ", 2);   // Repeat string
      echo strtoupper("hooray!");   // String to upper case
    ?>
    <?php
      echo fix_names("WILLIAM", "henry", "gatES");
    
      function fix_names($n1, $n2, $n3)
      {
        $n1 = ucfirst(strtolower($n1));
        $n2 = ucfirst(strtolower($n2));
        $n3 = ucfirst(strtolower($n3));
    
        return $n1 . " " . $n2 . " " . $n3;
      }
    ?>
    <?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
      $a1 = "WILLIAM";
      $a2 = "henry";
      $a3 = "gatES";
    
      echo $a1 . " " . $a2 . " " . $a3 . "<br>";
      fix_names($a1, $a2, $a3);
      echo $a1 . " " . $a2 . " " . $a3;
    
      function fix_names(&$n1, &$n2, &$n3)
      {
        $n1 = ucfirst(strtolower($n1));
        $n2 = ucfirst(strtolower($n2));
        $n3 = ucfirst(strtolower($n3));
      }
    ?>
  • 相关阅读:
    4.终端
    **如何让CI框架支持service层
    *CI框架装载器Loader.php源码分析
    如何在CI中写工具类,在哪个目录写
    ***在PHP语言中使用JSON和将json还原成数组(json_decode()的常见错误)
    DedeCMS常见问题和技巧
    ***Linux系统下如何查看及修改文件读写权限
    ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
    PHP5.2至5.6的新增功能详解
    PHP 5.2、5.3、5.4、5.5、5.6 对比以及功能详解
  • 原文地址:https://www.cnblogs.com/tszr/p/12380733.html
Copyright © 2011-2022 走看看