Jump to content

Differnet class vars


glenelkins

Recommended Posts

Hi

 

I must be getting boring on this forum now, i just keep coming accross new things i have'nt used in the past and just need some quick advice.

 

So in a class you have variables that can be used with the pointer $this. but what actually is the main difference between  a  "private" decleration, and a "protected"

Link to comment
https://forums.phpfreaks.com/topic/160485-differnet-class-vars/
Share on other sites

As always, the php.net documentation is the source of all the basic how to/what is php information -

Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item.
Link to comment
https://forums.phpfreaks.com/topic/160485-differnet-class-vars/#findComment-846901
Share on other sites

Simple example...

 

<?php
class A {
  private $myprivate = "private";
  protected $myprotected = "protected";
}

class B extends A {
  public function getPrivate(){
    return $this->myprivate;
  }

  public function getProtected(){
    return $this->myprotected;
  }
}

$test = new B();
$test->getProtected();
$test->getPrivate();

?>

 

If you run the above code you will see that class B cannot use the private variable from the class it extended, however it CAN use the protected variable.

Link to comment
https://forums.phpfreaks.com/topic/160485-differnet-class-vars/#findComment-846909
Share on other sites

because im being lazy!

 

i do know after testing if i want to use __construct as abstract, i have to actually have to replace the constructor of the abstract class with the same name function like the old way

 

for example

 

Abstract class Test {

 

    function Test() {

 

      do constructor bits

 

    }

 

    Abstract function __construct();

}

Link to comment
https://forums.phpfreaks.com/topic/160485-differnet-class-vars/#findComment-846945
Share on other sites

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.