Home php教程 PHP源码 全局对象_php教程

全局对象_php教程

May 23, 2016 am 08:39 AM

php代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<?php

function A($class){

    if(isset($GLOBALS[$class])){

        if(is_object($GLOBALS[$class])){

            return $GLOBALS[$class];

        }

    }

    return $GLOBALS[$class] = new $class();

}

 

class MyClass{

    public function getStr(){

        return "hh";

    }

    public function getStr1(){

        return "cc";

    }

    public function getJson(){

        return json_encode(array("name"=>"你好"));

    }

    public function getAll(){

        $str1 = A("MyClass")->getStr();

        $str2 = A("MyClass")->getStr1();

        $json = A("MyClass")->getJson();

        return $str1.$str2." ".$json;

    }

}

echo A("MyClass")->getAll();

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)