Skip to content

Instantly share code, notes, and snippets.

@adililhan
Created June 13, 2013 19:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adililhan/5776734 to your computer and use it in GitHub Desktop.
Save adililhan/5776734 to your computer and use it in GitHub Desktop.
PHP'de sınıf içinde bulunan closure fonksiyondan üye metota erişmeyi sağlayan kod.
<?php
class ClosureTest {
public $getInfo;
public function __construct() {
$that = $this; // Yerel degisken olusturuldu (local variable)
$this->getInfo = function() use($that) {
$that->getCity();
return 'Ben şehir bilgisini getiren metottan sonra çağırıldım.';
};
}
public function getCity() {
echo 'Angara';
}
}
$ClosureTest = new ClosureTest;
// Sinif icindeki closure fonksiyona erismek icin once bir degiskene atama yapmalisiniz.
$cT = $ClosureTest->getInfo;
echo $cT();
@sakirsensoy
Copy link

closure method'a erişmek için değişkene atama yapmak gereği bana çok saçma geldi. atama yapmadan kullanabilsek dadından yinmezdi :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment