downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

NULL> <Обекти
[edit] Last updated: Fri, 18 Sep 2009

view this page in

Ресурс

Ресурсът е специална променлива, която държи референция към външен източник. Ресурсите се създават и биват използвани посредством специални функции. Вижте приложението за списък с всички тези функции и съответните ресурсни типове.

Забележка: Типът ресурс беше въведен в PHP 4

Вж. също get_resource_type().

Преобразуване в ресурс

Тъй като ресурсите държат специални манипулатори към отворени файлове, връзки към бази от данни, изображения и т.н., преобразуването на каквато и да е стойност в ресурс не е възможно.

Освобождаване на ресурси

Благодарение на системата за броене на референции, въведена в Zend Engine на PHP 4, случаи, в които няма останали референции към даден ресурс, се откриват автоматично (също като в Java). В този случай всички източници, които са били използвани от този ресурс, се освобождават от боклукчията (garbage collector). Затова, рядко се налага паметта да бъде освобождавана ръчно, посредством някоя функция от рода free_result.

Забележка: Постоянните връзки към бази от данни са специални - те не се унищожават от боклукчията. Вижте и раздела относно постоянни връзки.



NULL> <Обекти
[edit] Last updated: Fri, 18 Sep 2009
 
add a note add a note User Contributed Notes Ресурс
Soos Gergely 26-Apr-2010 05:24
It is always wrong to assume that some operation, like casting to a resource makes no sense. People will always find some extreme case where it would be useful; like the previous example with mysql. My problem was that I wanted to start daemons from a web interface but the apache filehandles were inherited which caused that apache was unable to restart. If I could only typecast a number to a filehandle and then close it... Instead I had to write a small C program that closes every filehandle and then starts my program. I surely miss Apache2::SubProcess from perl. (Also, in perl you can reopen a file and then close it using IO::Handle module's fdopen. I'm just saying.)
wetmonkey__ at at __gmail dot com 15-Dec-2008 04:17
Resources are commonly used to iterate through a mysql or file handle.
example

<?php
while($row = mysql_fetch_row($resource)){
  echo
$row[0] ;
}
?>

It's possible to fake this treatment.

<?php
class fakewhile{

public
$arrayCount;
public
$arrayCounter;

function
setArrValues(){
  
$this->arrValues = array(0 =>array("apple","artichoke","apricot"),1 => array("bears","dogs","cats"));
 
$this->arrayCounter = 0;
 
$this->arrayCount = count($this->arrValues);
}

function
outputValues(){
/*
 * Anything until the if statement is evaluted one more
 * time then the array count value
 */
 
$arrayInfo = $this->arrValues;
 
$arrCounter = $this->arrayCounter;
 if(
$arrCounter > $this->arrayCount){
  return
false;
 }
 
$endCounter = $arrCounter+1;
 
$this->arrayCounter = $endCounter;
 return
$arrayInfo[$arrCounter];
}

}

$fw = new fakewhile();
$fw->setArrValues();
while(
$row = $fw->outputValues()){
   
print_r($row);
}
?>

Hopefully will get someone started on completing a complete application.
adrian dot dziubek at gmail dot com 07-Jul-2008 08:55
I spent an hour trying to create mock setup for testing SQL queries. The explanation here, that a resource contains file handlers and therefore there is no sense in trying to create one is lame. Being unable to redefine functions, creating a fake resource was the second thing I tried to put test in place, but looking at the search results, I see I'm the first one to try... For me it looks like security by obscurity.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites