Htaccess    html   Php    javascript   Asp   css    maths  Past Questions  Practice Tests Online

language.oop5.visibility function syntax tag tutorial 2013





Alert! Connect with 10,000 Chating Online. Join Now

Php language.oop5.visibility () function

Write your php code

 
<?php ?>

Php Result


Your code below



Visibility

The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.

Members Visibility

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

Example 19-8. Member declaration

<?php
/**
* Define MyClass
*/
class MyClass
{
    
public $public = 'Public';
    
protected $protected = 'Protected';
    
private $private = 'Private';

    function
printHello()
    {
        echo
$this->public;
        echo
$this->protected;
        echo
$this->private;
    }
}

$obj = new MyClass();
echo
$obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private


/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
    
// We can redeclare the public and protected method, but not private
    
protected $protected = 'Protected2';

    function
printHello()
    {
        echo
$this->public;
        echo
$this->protected;
        echo
$this->private;
    }
}

$obj2 = new MyClass2();
echo
$obj->public; // Works
echo $obj2->private; // Undefined
echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, not Private

?>

Note: The PHP 4 method of declaring a variable with the var keyword is no longer valid for PHP 5 objects. For compatibility a variable declared in php will be assumed with public visibility, and a E_STRICT warning will be issued.

Method Visibility

Class methods must be defined with public, private, or protected. Methods without any declaration are defined as public.

Example 19-9. Method Declaration

<?php
/**
* Define MyClass
*/
class MyClass
{
    
// Contructors must be public
    
public function __construct() { }

    
// Declare a public method
    
public function MyPublic() { }

    
// Declare a protected method
    
protected function MyProtected() { }

    
// Declare a private method
    
private function MyPrivate() { }

    
// This is public
    
function Foo()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate();
    }
}

$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work


/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
    
// This is public
    
function Foo2()
    {
        
$this->MyPublic();
        
$this->MyProtected();
        
$this->MyPrivate(); // Fatal Error
    
}
}

$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Private
?>

Php language.oop5.visibility Function syntax tag

language.oop5.visibility php code on this is provided for your study purpose, it will guide you to know how create and design a website using php. use it to practice and train your self online



Php language.oop5.visibility syntax tutorial

php tutorial guide and code design are for easy learning and programming. The code practice section provided at the top is for practising of this syntax. Use the code section up to practice your php programming online. Learning php is very easy, all you need is to use the examples on this site and practice them to perfect your skills.


Function index

Variables

Expressions

Operators

Control-Structures

Cookies

Array

Date & Time

Directory function

File

Image

Operators

Form data handling

Mathematics operators

Mail

Php Mysql

Network Functions

Strings

php tutorial guides,functions, classes, code examples and tags for creating simple dynamic site to mysql database driven sites
 
 
Htaccess    html   Php    javascript   Asp   css    maths  Past Questions  Practice Tests Online