Comunitatea PHP Romania
 

 
Visibility

Visibility

The visibility of a member or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be allow access to any caller. Protected limits access access to only classes inherited. Protected limits visiblity only to the class that defines the item.

Members Visibility

Class members must be defined with public, private, or private.

Exemplu 14-3. Member declaration

<?php

class MyClass {
   
public    $public     = "MyClass::public!\n";
   
protected $protected  = "MyClass::Protected!\n";
   
protected $protected2 = "MyClass::Protected2!\n";
   
private   $private    = "MyClass::private!\n";

   function
printHello() {
      print
"MyClass::printHello() " . $this->private;
      print
"MyClass::printHello() " . $this->protected;
      print
"MyClass::printHello() " . $this->protected2;
   }
}

class
MyClass2 extends MyClass {
   
protected $protected = "MyClass2::protected!\n";

   function
printHello() {

      
MyClass::printHello();    

      print
"MyClass2::printHello() " . $this->public;
      print
"MyClass2::printHello() " . $this->protected;
      print
"MyClass2::printHello() " . $this->protected2;

      
/* Will result in a Fatal Error: */
      //print "MyClass2::printHello() " . $this->private; /* Fatal Error */

   
}
}

$obj = new MyClass();

print
"Main:: " . $obj->public;
//print $obj->private; /* Fatal Error */
//print $obj->protected;  /* Fatal Error */
//print $obj->protected2;  /* Fatal Error */

$obj->printHello(); /* Should print */

$obj2 = new MyClass2();
print
"Main:: " . $obj2->private; /* Undefined */

//print $obj2->protected;   /* Fatal Error */
//print $obj2->protected2;  /* Fatal Error */

$obj2->printHello();
?>

Notã: The use PHP 4 use of declaring a variable with the keyword 'var' is no longer valid for PHP 5 objects. For compatiblity a variable declared in php will be assumed with public visiblity, and a E_STRICT warning will be issued.

Ultimele discutii in forum RSS Forum

Ultimele articole Ultimele articole

Top membri

Pirahna Pirahna
acasa , in pat
carco carco
Bucuresti
Birkoff Birkoff
Bucuresti
Mascka Mascka
Braila
raul_ raul_
whooper whooper
Toronto ON
mihaitha mihaitha
Sibiu
gabysolomon gabysolomon
Bacau
oriceon oriceon
Constanta
garlicinicolae garlicinicolae

Newsletter


Email:
 inscriere
 renuntare
 
 Arhiva newsletter

Parteneriat

Copyright © 2001-2008 PHP Romania Add PHPRomania to Google Add PHPRomania to Del.icio.us Add PHPRomania to Stumbleupon Add PHPRomania to Yahoo! Add PHPRomania to Digg Add PHPRomania to Blink Car Credit | Credit Cards | Debt Consolidation | Mortgage | Fragrance
Ads: Partener Way2Web Nework: gazduire web | inregistrare domenii | web design | imobiliare | web hosting
Powered by Simplis