Jump to content

PHP 5 private public... what the?


blueman378

Recommended Posts

In a nutshell, you have various keywords in PHP5 OOP.

 

private, public, abstract, protected etc. etc.

 

private means it can only be accessed by members of that class

public means it can be accessed by the class, parents/children of the class, or completely separate code (in other words, any code anywhere)

abstract means it can only be accessed by inherited classes

protected means it can only be accessed by children and parents of the class

 

For example:

 

<?php

class a {
    private $_variable = 'hi';
    public $var = 'hello';

    private function test() {
        echo 'showing private var $_variable:'. $this->_variable;
    }

    public function testing() {
        echo 'showing test through public function testing()...';
        $this->test();
    }
}

a::testing();   // Works
a::test();       // Doesn't work
$a = new a;
echo $a->_variable; // Doesn't work
echo $a->var;          // Works

?>

 

Excuse errors, I'll try to spot them asap

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.