3

I might not have a good understanding of this, but since the "username" variable is private. Shouldn't this not be a part of the return? How do I make it so the $username is private and not outputed, but the public member is?

class MyClass 
{
    private $username = "api";


    public function create_issue()
    {
        $this->public = "Joe";
        return $this;
    }

}

$test = new MyClass();
$test->create_issue();

var_dump($test);

class MyClass#1 (2) {
  private $username =>
  string(3) "api"
  public $public =>
  string(3) "Joe"
}
4
  • Have you tried to access it? Commented May 11, 2015 at 20:42
  • To avoid outputting it, simply don't use var_dump. That thing just spits out all info it can and should be used for debugging or logging. Also, you can't directly access the private property so try doing it, like others have suggested. Commented May 11, 2015 at 20:43
  • private means that other classes/external code can't access the variable, but it's still fully accessible to anything INSIDE the same class. just because var_dump can spit it out doesn't mean it's accessible. var_dump is for debugging purposes, and HAS to dump everything it can from whatever it's passed, which includes private stuff. Commented May 11, 2015 at 20:43
  • 1
    I like that question. var_dump is a function accessing a private member outside a class. And if you are building an API e.g. then your code exposes a security hole. Commented May 11, 2015 at 21:17

3 Answers 3

3

I have understood your concern. First of all, let me discuss about the variable scope private. private variable is private in the current class. If you use the class in another class private variable will not work. So, you have to use an another class to protect your private variable.

<?php

class MyClassProtected 
{
    private $username = "api";
    public function doSomething(){   
    // write code using private variable
   }

}


class MyClass
{   
    public function create_issue()

    {
       $test = new MyClassProtected();
       $test -> doSomething();
       return $this->public = "Joe";
    }
}


$test = new MyClass();
$test->create_issue();
var_dump($test);

?>
Sign up to request clarification or add additional context in comments.

Comments

2

Note: Don't ever use var_dump to render your class unless it's for debugging purposes.

Even though this is not the intent of private scope, interestingly enough, you can use echo json_encode($object); and the private variables within the class will not be outputted. You can then safely use this JSON in your API.

class MyClass 
{
    private $username = "api";


    public function create_issue()
    {
        $this->public = "Joe";
        return $this;
    }

}

$test = new MyClass();
$test->create_issue();

echo json_encode($test); // prints {"public":"Joe"}

Read up more on proper private/protected/public usage here

Comments

0
<?php class Demo {
private $string_var='yes';
protected $int_var=50000;
public $float_var=12.000;

public function __debugInfo() 
{
    return [
        'propString' => $this->string_var.'No',
    ];
} } var_dump(new Demo());    ?>

https://www.php.net/manual/en/language.oop5.magic.php#object.debuginfo

4 Comments

Welcome to StackOverflow! Please elaborate your answer, adding some brief explaination of this code and why it's better than other answers here.
__debugInfo is one of the magic functions in PHP. This method is used in class.
Welcome to SO! @Denis Sheremet is right. The live link can disappear and just code is an incomplete answer as you should expose what are the Pros of yours as well as how it works.
Thanks, I will do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.