GithubHelp home page GithubHelp logo

Comments (5)

xianyunyh avatar xianyunyh commented on May 19, 2024

在PHP中,匿名函数的作用,大部分作用是作为一次性函数,回调,很少作为向JavaScript那样使用闭包

$fn = function(){};

$fn();

array_map(function(){},$array);

javascript中 为了保存内部变量

var f = function(){
   $a=1
//产生闭包
   return function(){
   return $a++
}
}

f1 = f()
f1()//1
f1()//2

from php-interview.

bestcyt avatar bestcyt commented on May 19, 2024

嗯嗯,对的,js中闭包是能访问到外部变量的,而PHP需要use;我的意思是在那句话前加个(在PHP中),会不会比较严谨点?哈哈哈我就无聊随便说说:)

from php-interview.

xianyunyh avatar xianyunyh commented on May 19, 2024

没事! @bestcyt 相互交流而已。 PHP的匿名函数 一般就是作回调。Js的匿名函数只要是为了保存一些内部变量,产生闭包。 js的作用域和PHP的也不太一样,所以两个语言相互理解一下

$a = 10;//外部变量 非全局 
function f () {
echo $a; //内部的$a 未定义
}

# use 将a加到函数的作用域下
$f1 = function () use ($a) {
    echo $a;
};
$f1();
var a =  10 //全局变量
function f(){
alert(a)//10
}

from php-interview.

bestcyt avatar bestcyt commented on May 19, 2024

image
@xianyunyh 大佬这边子类应该是不能用parent来获取父类的保护属性,要用this,你看看是不是酱紫?

from php-interview.

xianyunyh avatar xianyunyh commented on May 19, 2024

@bestcyt 可能我写错了! 以下是用法和官方的介绍。

感谢指正

范围解析操作符(也可称作 Paamayim Nekudotayim)或者更简单地说是一对冒号 ::,可以用于访问 静态成员类常量,还可以用于覆盖类中的属性和方法。

class A
{
    const PI = 3.14;
    public static $static_var = "A的静态变量";
    public function __construct()
    {
        echo "this is A" . "\n";
    }
}

class B extends A
{

    public function __construct()
    {
        parent::__construct();
    }
    public function test()
    {
        echo parent::PI . "\n";
        echo parent::$static_var;
    }
}

(new B())->test();

参考链接

from php-interview.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.