美文网首页
2018-06-20

2018-06-20

作者: markstudio | 来源:发表于2018-06-20 08:08 被阅读0次

    PHP标记四种风格:

    // XML风格
    <?php echo '<p>Order processed</p>'; ?>
    // 简短风格
    <? echo '<p>Order processed</p>'; ?>
    // script style
    <script language='php'> echo '<p>Order processed</p>'; </script>
    // ASP style, 默认此风格是禁用的
    <% echo '<p>Order processed</p>'; %>
    

    echo print print_r
    一个或多个字条 简单变量的值 复杂类型变量的值(如: array, object)
    速度最快 N/A N/A
    无返回值 返回int 返回boolean

    var_dump($variable)

    <?php 
    // int
    $x = 9895;
    var_dump($x);
    // boolean
    $y = false;
    var_dump($y);
    // null
    $nul = null;
    var_dump($nul);
    // array
    $z = array("Volvo", "BMW", "Toyota");
    var_dump($z);
    

    constant: 常量不需要使用$符号;

    <?php 
        define("GREETING", "constant value");
    // ture means NOT case sensitive
    //    define("GREETING", "constant value", true);
        echo GREETING;
    
        // in class
    class MyClass {
        const constant = '常量值';
        function showConstant() {
            // do not use $this, must "self"
            echo self::constant.PHP_EOL;
        }
    }
    

    class

    <?php 
    class Car {
        var $color;
        
        function Car($color = "green") {
            $this->color = $color;
        }
    
        function what_color() {
            return $this->color;
        }
    }
    //
    class phpClass {
        var $var1;
        var $var2;
        var $var3 = "constant string";
    
        // 构造函数
        function __construct($par1, $par2) {  
            $this->var1 = $par1, 
            $this->var2 = $par2;
        }   
        // 析构函数
        function __destruct(void) {
            // destruct some member parameter
        }
    
        function myFunc($arg1, $arg2) {
            // some code here
        }
        // more functions
    }
    

    public, protected, private

    class MyClass {
        var $var = 'var';              // default is public
        public $public = 'Public';
        protected $protected = 'Protected';
        private $private = 'Private';
        
        // default is public
        function myFunction() {
        }
        // public method
        public function publicFunction() {
        }
        // protected method
        protected function protectedFunction() {
        }
        // private method
        private function privateFunction() {
        }
    }
    

    interface:指定某个class必须实现哪些方法,接口中定义的方法必须都是公有的:

    <?php
    interface iTemplate {
        public function setVariable($name, $var);
        public function getHtml($template);
    }
    
    class Template implements iTemplate {
        public function setVariable($name, $var) {
        }
    
        public function getHtml($template) {
        }
    }
    

    abstract class: 至少一个方法声明为抽象, 这样的类不能实例化

    <?php
    abstract class AbstractClass {
        // just declaration
        abstract protected function prefixValue();
    }
    
    class ConcreteClass1 extends AbstractClass {
        protected function prefixValue() { // some code here }
    }
    

    相关文章

      网友评论

          本文标题:2018-06-20

          本文链接:https://www.haomeiwen.com/subject/vmltyftx.html