PHP private property should not be accessible in Child class - php

I have one silly doubts.
I'm setting username property as private in user class
But According to the rules private property should not be accessible
in Child class or inherited and outside of the class but it's happening
in my code. I just want to know where i did wrong.
<?php
class User{
private $username;
}
class UserRepository extends User{
public function get(){
return $this->username;
//this should give error can't access private property
}
public function set($username){
$this->username=$username;
//this should give error can't access private property
}
}
$UserReposetry =new UserRepository;
$UserReposetry->username='daulat';//this should give error.
echo $UserReposetry->username;//this should give error.
It should not work but it's working.

No way,
Here see the code and see result (error)
<?php
class User{
private $username="aaa";
}
class UserRepository extends User{
public function get(){
return $this->username;
}
}
$UserReposetry =new UserRepository;
echo $UserReposetry->get();//this should give error.
Output:
Notice: Undefined property: UserRepository::$username in C:\Users...........index.php on line 7
(To see this notice you have to set your error reporting as E_ALL)
And line 7 means return $this->username; , try using ::parent keyword too
In Your Code: You are just setting a new variable. So don't misunderstand that code is using private member.

Related

PHP private accessor

I made a private variable in my class with a set and get functions but I keep on getting the following error:
Fatal error: Uncaught Error: Cannot access private property Car::$make
in C:\xampp\htdocs\PhpOOP\index.php:16 Stack trace: #0 {main} thrown
in C:\xampp\htdocs\PhpOOP\index.php on line 16
When I change it from private to public it works fine.
Here is my class:
<?php
class Car {
private $make;
public $model;
public $color;
public function starting(){
echo "Car Starting";
}
public function setMake($make){
$this->make = $make;
}
public function getMake(): string{
return $this->make;
}
}
And here is where I am creating an instance of the class and trying to use my methods.
<body>
<?php
include "classes/Car.php";
$car1->setMake("Honda");
echo $car1->getMake();
?>
</body>
You are not instantiating your class variable. You are including your class file, but you then need to instantiate the class
<body>
<?php
include "classes/Car.php";
$car1 = new Car();
$car1->setMake("Honda");
echo $car1->getMake();
?>
</body>
Fixed I was progressively learning objects in php. Was accessing the private variable when it was public. Then I turned it private to see how that worked. And my old code that was accessing it was causing the problem not the new stuff with the set and get methods.

What is the differece between private and protected in OOP?

I don't understand what is the different between private method and protected method in Object Oriented PHP. After make a method private , I'm able to access it from extends class. Please check the code below -
<?php
class person{
private function namedilam(){
return "likhlam";
}
public function kicu(){
return $this->namedilam();
}
}
class second extends person{
}
$info = new second;
echo $info->kicu();
The difference will become clear when you do it like this:
class Liam {
private getFirstName() {
return "Liam";
}
public function getName() {
return $this->getFirstName();
}
}
class Max extends Liam {
private function getFirstName() {
return "Max";
}
}
class Peter extends Liam {
public function getLiamsName() {
return $this->getFirstName();
}
}
$max = new Max();
echo $max->getName();
// returns "Liam", not "Max" as you might expect
$peter = new Peter();
echo $peter->getLiamsName();
// PHP Fatal error: Uncaught Error: Call to private method Liam::getFirstName() [...]
Max will return "Liam" because the getName() calls getFirstName() in the Liam class, not the one from the class extending it. This means with private methods you can make sure that whenever in your class you call this method exactly this one is used and it will never be overwritten.
To explain it in general terms:
private methods are only accessible inside the class. They can not be overwritten or accessed from outside or even classes extending it.
protected methods are accessible inside the class and in extending classes, but you can't call them from outside like:
$max = new Max();
$max->iAmProtected();
This will neither work with private or protected methods.

PHP change method/function visibility

I am trying to write a PHP class in which I change the visibility of a few methods from protected to public. I believe I remember you can do this in C++, but I did a few searches and I am not coming up with anything for that in PHP. Does anyone know if this is even possible in PHP?
For example, suppose this class:
class ABC {
protected function foo() {
// Do something
}
}
class DEG extends ABC {
// can I make foo public now?
}
You can change the visibility of members when deriving from a base class like this:
class Base
{
protected function foo() {}
}
class Derived extends Base
{
public function foo() { return parent::foo(); }
}
You can also do the same with properties (redefine a protected property as public).
However, be aware that if the base property is private then you will not actually increase its accessibility but rather declare a new property with the same name. This is not an issue with functions, as if you tried to call a private base method you would immediately get a runtime error.
You can overwrite a method in a derived class to highten it´s visibility (e.g. protected->public). Make the new function return it´s parent.
You cannot do so to limit it´s visibility (e.g. public->protected), but you can implement a method that checks the backtrace for the caller and thwors an exception if it´s a foreign class.
You can always use the reflection API to do all kinds of changes to the visibility.
Yes, it can be done. Quoting from PHP manual..
The visibility of a property or method can be defined by prefixing the
declaration with the keywords public, protected or private. Class
members declared public can be accessed everywhere. Members declared
protected can be accessed only within the class itself and by
inherited and parent classes. Members declared as private may only be
accessed by the class that defines the member.
And the example from there as well..
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
Edit : Yes, you can change visibility of public and protected members. Another example from PHP manual..
/**
* 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 $obj2->public; // Works
echo $obj2->private; // Undefined
echo $obj2->protected; // Fatal Error
$obj2->printHello(); // Shows Public, Protected2, Undefined
?>

PHP Private Variable Access

This is a very silly question, but I don't know what's wrong. I can't get the value of a private variable through a public method. I'm using CodeIgniter.
class someClass extends MY_Model {
private $table = 'sometable';
public function getTable() {
return $this->table;
}
public function updateTable($data) {
$this->db->update($this->getTable(), $data);
}
}
When I call this method from the controller, I get this message:
Fatal error: Access level to someClass::$table must be public (as in class MY_Model) in /some/path/someclass.php on line 38
What have I done wrong? Thank you.
Your parent class MY_Model is declaring that field with public scope, so you must adhere to that in your child class.

private class Error Undefined property

Good morning everybody, I have a two class, the first accesses the second class.
More on segunta class has a private $ my, and this gives the error Undefined property: session::$my in line, if($this->my)
I would be very grateful for the help.
Sample code,
class session{
public function run_session(){
..run..
data::run($line);
}
}
class data {
private $my = "../../my/";
public function run($line){
if($this->my.$line){
....run...
}
}
}
you must use like this
class data {
private $my = "../../my/";
public function run($line){
if($this->my.$line){ // here you are using $this, so the function must be called on object of class data
....run...
}
}
}
class session{
public function run_session(){
..run..
$data = new data(); // create object of class data, so that you can call the function run
$data->run($line);
}
}

Categories