PHP垃圾收集 [英] Php garbage collection

查看:105
本文介绍了PHP垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你,在PHP中,如果我在函数中创建了一个新类,那么会在函数结束时释放内存吗?

例如: Class

  class config 
{
var $ mapProp = array(x=> 4333, y=> 3520,w=> 128,h=> 128);
var $ gameProp = array(maxLevel=> 14,tilesSize=> 256);
var $ mapUrl ='map_files';
函数getMapProp()
{
return $ this-> mapProp;
}
函数getGameProp()
{
return $ this-> gameProp;
}
函数getMapUrl()
{
return $ this-> mapUrl;
}
}
$ config = new config();

和函数

  class framework 
{
function getUserMap()
{
require(class / config / config.php);
require(class / imageManipulation / image.php);

$ mapUrl = $ config-> getMapUrl();
$ x = $ _GET ['x'];
$ y = $ _GET ['y'];
$ level = $ _GET ['level'];

$ main_img = $ mapUrl。'/'。$ level。'/'。$ x .'_'。$ y。'。jpg';
//创建一个新类
$ image = new imageManipulation();

//设置变量
$ image-> setProp($ config-> getMapProp());
$ image-> setGameProp($ config-> getGameProp());

返回$ image-> getImage($ main_img,$ level,$ x,$ y);
}
}
$ framework = new framework();

require(class / framework / framework.php);

$ path = $ _GET ['path'];

switch($ path){
case'usermap':
$ framework-> getUserMap();
休息;
}

我已经使用了两个类。其中一个是$ config类,另一个是$ image类,在函数结束后,用于这两个类的内存将被释放?



所有最好,
罗伯特。 是的。局部变量放置在函数调用的末尾。如果其中一个局部变量是一个对象,它就不再被引用。因此它会被垃圾收集器释放。


I want to ask you, in PHP if I created a new class in a function, will the memory freed in the end of the function?

For example: Class

class config
{
    var $mapProp    =   array("x"=>4333, "y"=>3520, "w"=>128, "h"=>128);
    var $gameProp   =   array("maxLevel"=>14, "tilesSize"=>256);
    var $mapUrl     =   'map_files';
    function getMapProp()
    {
        return $this->mapProp;
    }
    function getGameProp()
    {
        return $this->gameProp;
    }
    function getMapUrl()
    {
        return $this->mapUrl;
    }
}
$config = new config();

and the function

class framework
{
    function getUserMap()
    {
        require("class/config/config.php");
        require("class/imageManipulation/image.php");       

        $mapUrl     =   $config->getMapUrl();
        $x          =   $_GET['x'];
        $y          =   $_GET['y'];
        $level      =   $_GET['level'];

        $main_img   =   $mapUrl.'/'.$level.'/'.$x.'_'.$y.'.jpg'; 
        //Create a new class
        $image = new imageManipulation();

        //Set Up variables
        $image->setProp($config->getMapProp());
        $image->setGameProp($config->getGameProp());

        return $image->getImage($main_img, $level, $x, $y);
    }       
}
$framework = new framework();

require("class/framework/framework.php");

$path       =   $_GET['path'];

switch ($path) {
    case 'usermap':
        $framework->getUserMap();
        break;
}

in the getUserMap I have used two classes. One of them is the $config class the other is the $image class, after the end of the function will the memory used for this two classes be freed?

All the best, Robert.

解决方案

Yes it does. Local variables are disposed at the end of the function call. And if one of these local variables was an object, it is no longer referenced. Therefore it will get freed by the garbage collector.

这篇关于PHP垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆