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.
Related
I have a class address_book in which I want to set $DBConnection using method from another class:
<?php
class address_book
{
protected $DBConnection;
public function __construct()
{
$this->DBConnection=new address_book_db();
$this->init();
}
public function init()
{
add_shortcode('address_book', array($this,'load'));
}
public function load()
{
var_dump($DBConnection);
}
}
Another class:
<?php
class address_book_db
{
protected $dbconnection;
public function __construct()
{
$this->dbconnection='1';
}
}
So var_dum should return '1' as it has to be assigned to protected $DBConnection; in the first class. I'm starting my learning of PHP OOP so probably everything is bad.
Any way it has to work like this.
In first class I load db name which is being loaded from another class using methods which determines db name (not developed yet because I just want to pass this constructed dbname to first class).
You're missing $this-> to refer to the class property.
The property's value contains another class, so you have to refer to that class its property as wel with a second ->
var_dump($this->DBConnection->dbconnection)
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.
I'm trying to access an object of a class in another extended class.
class MainClass{
protected $theobject;
function __construct(){
$this->theobject = new AnotherClass();
}
}
class TheClass extends MainClass{
function AnotherFunction(){
$this->theobject->SomeFunction();
}
}
I'm getting an error on $this->theobject->AnotherFunction(). The error is "Call to a member function SomeFunction() on a non-object".
But this works fine:
class TheClass{
protected $theobject;
function SoemFunction(){
$this->theobject = new AnotherClass();
$this->theobject->SomeFunction();
}
}
Is it even legal to do this in PHP?
Pretty much all the code: http://3v4l.org/MaXe6
When i var_dump the $this->theobject in TheClass is comes back as NULL.
class TheClass extends MainClass{
function __construct(){
parent::__construct();
}
function SoemFunction(){
$this->theobject->SomeFunction();
}
}
Do the construct method when init the child class.
Having the following class hierarchy:
class TheParent{
public function parse(){
$this->validate();
}
}
class TheChild extends TheParent{
private function validate(){
echo 'Valid!!';
}
}
$child= new TheChild();
$child->parse();
What is the sequence of steps in which this is going to work?
The problem is when I ran that code it gave the following error:
Fatal error: Call to private method TheChild::validate() from context 'TheParent' on line 4
Since TheChild inherits from TheParent shouldn't $this called in parse() be referring to the instance of $child, so validate() will be visible to parse()?
Note:
After doing some research I found that the solution to this problem would either make the validate() function protected according to this comment in the PHP manual, although I don't fully understand why it is working in this case.
The second solution is to create an abstract protected method validate() in the parent and override it in the child (which will be redundant) to the first solution as protected methods of a child can be accessed from the parent?!!
Can someone please explain how the inheritance works in this case?
Other posters already pointed out that the mehods need to be protected in order to access them.
I think you should change one more thing in your code. Your base class parent relies on a method that is defined in a child class. That is bad programming. Change your code like this:
abstract class TheParent{
public function parse(){
$this->validate();
}
abstract function validate();
}
class TheChild extends TheParent{
protected function validate(){
echo 'Valid!!';
}
}
$child= new TheChild();
$child->parse();
creating an abstract function ensures that the child class will definitely have the function validate because all abstract functions of an abstract class must be implemented for inheriting from such a class
Your idea of inheritence is correct, just not the visibility.
Protected can be used by the class and inherited and parent classes, private can only be used in the actual class it was defined.
Private can only be accessed by the class which defines, neither parent nor children classes.
Use protected instead:
class TheParent{
public function parse(){
$this->validate();
}
}
class TheChild extends TheParent{
protected function validate(){
echo 'Valid!!';
}
}
$child= new TheChild();
$child->parse();
FROM PHP DOC
Visibility from other objects
Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects.
Private can only be accessed by the class which defines or Same object type Example
class TheChild {
public function parse(TheChild $new) {
$this->validate();
$new->validate(); // <------------ Calling Private Method of $new
}
private function validate() {
echo 'Valid!!';
}
}
$child = new TheChild();
$child->parse(new TheChild());
Output
Valid!!Valid!!
This is my first OOP php app and I'm getting a little stumped here...
I created the following class that extends the CI_Model
class LXCoreModel extends CI_Model{
function __construct() {
parent::__construct();
}
public function elementExists($table,$row,$data){
$result = $this->db->select('*')->from($table)->where($row, $data)->get()->result();
if(empty($result))return false;
return true;
}
}
And here is the class extending the class above:
class LXAccAdminModel extends LXCoreModel{
function __construct()
{
parent::__construct();
}
function addAccountStatus($statusId=NULL, $username=NULL){
if($statusId==NULL)$statusId = $this->input->post('accountStatusId');
if($username==NULL)$username = $this->input->post('username');
if(elementExists('accounts','username',$username))
if(elementExists('statuses','id',$statusId))
{$this->db->insert('accountstatus',array('statusid'=>$statusId,'username'=>$username)); return true;}
return false;
}
}
Both classes are in the Model diretory, and the class LXCoreModel is autoloaded (the line $autoload['model'] = array('LXCoreModel'); exists in the autoload.php file) and yet, when I try to run my code I get this error:
Fatal error: Call to undefined
function elementExists() in
C:\wamp\www\CI_APP\application\models\LXAccAdminModel.php
on line 25
Thanks for your time! :)
You're calling elementExists(), but not as a method of the class.
Try:
$this->elementExists();
Or from LXAccAdminModel:
parent::elementExists();
$this->elementExists() should suffice in both cases, $this referring to the current class.
if i am not wrong then the error is in your derived class you have forgot to put $this while calling the elementExists() function that should be $this->elementExists()