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

search for in the

Sınıf Sabitleri> <Temel Kavramlar
[edit] Last updated: Fri, 23 Dec 2011

view this page in

Özellikler

Sınıf üyesi değişkenlere "özellik" denir. Bunlara bazan "öznitelik" veya "alan" dendiğini de görürsünüz, fakat bu kılavuzun amaçları doğrultusunda biz "özellik" terimini kullanacağız. Bunlar normal değişken bildiriminin önüne public, protected, veya private anahtar sözcükleri getirilerek bildirilir. Bildirim, bir ilklendirme olarak da yapılabilir; bu durumda bir sabit değerle ilklendirme yapılmalıdır. Yani, derleme sırasında değerlendirilebilmeli, çalışma anında değerlendirilebilecek bilgiler içermemelidir.

public, protected, ve private sözcüklerinin anlamları için Görünürlük belgesine bakınız.

Bilginize:

PHP 4 ile geriye uyumluluğu sağlamak için, PHP 5 özellik bildiriminde public, protected, veya private yerine (veya ek olarak) hala var sözcüğü kabul edilmektedir. Ancak, var artık gerekli değildir. PHP'nin 5.0'dan 5.1.3'e kadar olan sürümlerinde, var kullanımı önerilmeyen kullanım olarak ele alınır ve bir E_STRICT uyarısı çıktılanırdı; PHP 5.1.3'ten beri önerilmeme durumu ortadan kalkmış olup bir uyarı çıktılanmamaktadır.

Bir özelliği public, protected, veya private yerine var kullanarak bildirirseniz PHP 5 özelliği public olarak bildirilmiş gibi ele alır.

Özelliklere, sabitlere ve yöntemlere sınıf yöntemleri içinden erişilirken yöntem duruk ise self::$özellik, değilse $this- >özellik sözdizimi kullanılabilir (burada "özellik" özelliğin ismidir). Daha ayrıntılı bilgi için static Anahtar Sözcüğü bölümüne bakınız.

Yöntem bir nesne bağlamından çağrılıyorsa $this sözde değişkeni sınıf yöntemleri içinde de kullanılabilir. $this, yöntemi çağıran nesneye (yöntemin ait olduğu nesneye) bir gönderimdir. Fakat yöntem, bir ikincil nesne bağlamından duruk olarak çağrılıyorsa, bu, birincil nesne de olabilir.

Örnek 1 - Özellik bildirimi

<?php
class SimpleClass
{
   
// geçersiz özellik bildirimleri:
   
public $var1 'hello ' 'world';
   public 
$var2 = <<<EOD
hello world
EOD;
   public 
$var3 1+2;
   public 
$var4 self::myStaticMethod();
   public 
$var5 $myVar;

   
// geçerli özellik bildirimleri:
   
public $var6 myConstant;
   public 
$var7 = array(truefalse);

   
// Buna sadece PHP 5.3.0 ve sonrasında izin verilir.
   
public $var8 = <<<'EOD'
hello world
EOD;
}
?>

Bilginize:

Sınıflar ve nesnelerle çalışan bazı işlevler vardır. Bunlar için Sınıf ve Nesne İşlevleri bölümüne bakınız.

Yorumsuz metinler, yorumlu metinlerin tersine özellik bildirimleri dahil herhangi bir duruk veri bağlamında kullanılabilir.

Örnek 2 - Özellik ilklendirmede yorumsuz metin kullanımı

<?php
class foo {
   
// PHP 5.3.0'dan beri
   
public $bar = <<<'EOT'
bar
EOT;
}
?>

Bilginize:

Yorumsuz metin desteği PHP 5.3.0'da eklenmiştir.



Sınıf Sabitleri> <Temel Kavramlar
[edit] Last updated: Fri, 23 Dec 2011
 
add a note add a note User Contributed Notes Özellikler
Anonymous 11-Mar-2011 03:18
$this can be cast to array.  But when doing so, it prefixes the property names/new array keys with certain data depending on the property classification.  Public property names are not changed.  Protected properties are prefixed with a space-padded '*'.  Private properties are prefixed with the space-padded class name...

<?php

class test
{
    public
$var1 = 1;
    protected
$var2 = 2;
    private
$var3 = 3;
    static
$var4 = 4;
   
    public function
toArray()
    {
        return (array)
$this;
    }
}

$t = new test;
print_r($t->toArray());

/* outputs:

Array
(
    [var1] => 1
    [ * var2] => 2
    [ test var3] => 3
)

*/
?>

This is documented behavior when converting any object to an array (see </language.types.array.php#language.types.array.casting> PHP manual page).  All properties regardless of visibility will be shown when casting an object to array (with exceptions of a few built-in objects).

To get an array with all property names unaltered, use the 'get_object_vars($this)' function in any method within class scope to retrieve an array of all properties regardless of external visibility, or 'get_object_vars($object)' outside class scope to retrieve an array of only public properties (see: </function.get-object-vars.php> PHP manual page).
zzzzBov 04-Jun-2010 09:21
Do not confuse php's version of properties with properties in other languages (C++ for example).  In php, properties are the same as attributes, simple variables without functionality.  They should be called attributes, not properties.

Properties have implicit accessor and mutator functionality.  I've created an abstract class that allows implicit property functionality.

<?php

abstract class PropertyObject
{
  public function
__get($name)
  {
    if (
method_exists($this, ($method = 'get_'.$name)))
    {
      return
$this->$method();
    }
    else return;
  }
 
  public function
__isset($name)
  {
    if (
method_exists($this, ($method = 'isset_'.$name)))
    {
      return
$this->$method();
    }
    else return;
  }
 
  public function
__set($name, $value)
  {
    if (
method_exists($this, ($method = 'set_'.$name)))
    {
     
$this->$method($value);
    }
  }
 
  public function
__unset($name)
  {
    if (
method_exists($this, ($method = 'unset_'.$name)))
    {
     
$this->$method();
    }
  }
}

?>

after extending this class, you can create accessors and mutators that will be called automagically, using php's magic methods, when the corresponding property is accessed.
Anonymous 17-Nov-2009 07:51
As of PHP 5.3.0, heredocs can also be used in property declarations.

<?php
class foo {
  
// As of PHP 5.3.0
  
public $bar = <<<EOT
bar
EOT;
}
?>

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