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

search for in the

Tür Dönüşümü> <NULL
Last updated: Fri, 06 Nov 2009

view this page in

Bu belgede kullanılan sözde türler ve değişkenler

mixed

mixed anahtar sözcüğü, bir değiştirgenin çok sayıda tür (ama hepsini değil) kabul edebileceğini belirtir.

Örneğin, str_replace() işlevi sadece string veya array türünde değer kabul ederken, gettype() işlevi tüm PHP türlerini kabul eder.

number

number anahtar sözcüğü, bir değiştirgenin integer veya float türünde değer kabul edeceğini belirtir.

callback

call_user_func(), usort() gibi işlevler değiştirge olarak kullanıcı tanımlı geriçağırım işlevlerini kabul ederler. Geriçağırım işlevleri her zaman basit işlevler olmayabilir, nesne yöntemleri ve hatta duruk sınıf yöntemleri bile olabilirler.

Bir PHP işlevi, string türündeki ismiyle aktarılır. Şu dil oluşumları dışında herhangi bir yerleşik veya kullanıcı tanımlı işlev kullanılabilir: array(), echo(), empty(), eval(), exit(), isset(), list(), print() veya unset().

Örneklenmiş bir nesnenin bir yöntemi, nesnenin ismi 0. indiste, yöntem ismi 1. indiste yer alan bir dizi olarak aktarılır.

Duruk sınıf yöntemleri de sınıf ismi 0. indiste içerilerek, sınıf bir nesne olarak örneklenmeksizin aktarılabilir.

create_function() işlevi bildik kullanıcı tanımlı işlevlerden başka, bir anonim geriçağırım işlevi oluşturmak için de kullanılabilir. PHP 5.3.0'dan itibaren işleve değiştirge olarak bir anonim işlev aktarmak mümkün oldu.

Örnek 1 - Geriçağırım işlevi örnekleri

<?php

// Bir geriçağırım işlevi örneği
function geriçağırım_işlevim() {
    echo 
'merhaba dünya!';
}

// Bir geriçağırım yöntemi örneği
class Sınıfım {
    static function 
geriçağırımYöntemim() {
        echo 
'Merhaba Dünya!';
    }
}

// 1. tür: Basit geriçağırım
call_user_func('geriçağırım_işlevim');

// 2. tür: Duruk sınıf yöntemi çağrısı
call_user_func(array('Sınıfım''geriçağırımYöntemim'));

// 3. tür: Nesne yöntemi çağrısı
$nesne = new Sınıfım();
call_user_func(array($nesne'geriçağırımYöntemim'));

// 4. tür: Duruk sınıf yöntemi çağrısı (PHP 5.2.3 ve sonrası)
call_user_func('Sınıfım::geriçağırımYöntemim');

// 5. tür: Göreli duruk sınıf yöntemi çağrısı (PHP 5.2.3 ve sonrası)
class {
    public static function 
kimsin() {
        echo 
"A\n";
    }
}

class 
extends {
    public static function 
kimsin() {
        echo 
"B\n";
    }
}

call_user_func(array('B''parent::kimsin')); // A
?>

Örnek 2 - Anonim işlev kullanan bir geriçağırım işlevi örneği

<?php
// Anonim işlevimiz
$double = function($a) {
    return 
$a 2;
};

// Sayı aralığımız
$numbers range(15);

// Aralık içindeki her elemanın boyutunu
// ikiye katlamak için geriçağırım işlevi
// olarak burada anonim bir işlev kullanalım
$new_numbers array_map($double$numbers);

print 
implode(' '$new_numbers);
?>

Yukarıdaki örneğin çıktısı:

2 4 6 8 10

Bilginize: PHP4'te, kopyayı değil de asıl nesneyi işaret eden bir geriçağırım oluşturmak için gönderim kullanmak gerekir. Daha fazla bilgi için Gönderimler Hakkında bölümüne bakınız.

void

void bir dönüş türü olarak dönüş değerinin yararsız olduğu anlamına gelir. void bir değiştirge listesinde kullanıldığında işlevin değiştirge kabul etmediği anlamına gelir.

... (üçlü nokta)

$... değiştirgesi işlev bildirimlerinde "ve benzerleri" anlamına gelir. Bu değişken ismi bir işlevde kullanıldığında işlev sonsuz sayıda değiştirge alabilir.



Tür Dönüşümü> <NULL
Last updated: Fri, 06 Nov 2009
 
add a note add a note User Contributed Notes
Bu belgede kullanılan sözde türler ve değişkenler
michael dot martinek at gmail dot com
29-Aug-2009 04:20
The documentation is a little confusing, and with the recent OO changes it adds a little more to the confusion.

I was curious whether you could pass an object through the user func, modify it in that callback and have the actual object updated or whether some cloning was going on behind the scenes.

<?php
   
class Test
   
{
        var
$sValue = 'abc';

        function
testing($objTest)
        {
           
$objTest->sValue = '123';
        }
    }

   
$obj = new Test();

   
call_user_func(array($obj, 'testing'), $obj);

   
var_dump($obj);

?>

This works as expected: The object is not cloned, and $sValue is properly set to '123'. With the OO changes in PHP 5, you don't need to do "function testing(&$objTest)" as it is already passed by reference.
phpguy at lifetoward dot com
12-Jun-2009 12:44
I noticed two important thing about putting callbacks into an arg list when calling a function:

1. The function to which the callback refers must be defined earlier in the source stream. So for example:

function main() {...; usort($array, 'sortfunction'); ... }
function sortfunction($a, $b){ return 0; }

Will NOT work, but this will:

function sortfunction($a, $b){ return 0; }
function main() {...; usort($array, 'sortfunction'); ... }

2. It's not really just a string. For example, this doesn't work:

usort($array, ($reverse?'reversesorter':'forwardsorter'));

I found these two discoveries quite counterintuitive.
sahid dot ferdjaoui at gmail dot com
20-Apr-2009 10:19
An example with PHP 5.3 and lambda functions

<?php

  array_map
(function ($value) {
    return new
MyFormElement ($value);
  },
$_POST);

?>
Hayley Watson
24-May-2007 05:44
The mixed pseudotype is explained as meaning "multiple but not necessarily all" types, and the example of str_replace(mixed, mixed, mixed) is given where "mixed" means "string or array".
Keep in mind that this refers to the types of the function's arguments _after_ any type juggling.
levi at alliancesoftware dot com dot au
08-Feb-2007 10:44
Parent methods for callbacks should be called 'parent::method', so if you wish to call a non-static parent method via a callback, you should use a callback of
<?
 
// always works
 
$callback = array($this, 'parent::method')

 
// works but gives an error in PHP5 with E_STRICT if the parent method is not static
 
$callback array('parent', 'method');
?>
Edward
01-Feb-2007 10:15
To recap mr dot lilov at gmail dot com's comment: If you want to pass a function as an argument to another function, for example "array_map", do this:

regular functions:
<?
array_map
(intval, $array)
?>

static functions in a class:
<?
array_map
(array('MyClass', 'MyFunction'), $array)
?>

functions from an object:
<?
array_map
(array($this, 'MyFunction'), $array)
?>

I hope this clarifies things a little bit

Tür Dönüşümü> <NULL
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites