9

so I'm trying to work out an issue I'm having in designing PHP classes. I've created a base class, and assigned private variables. I have child classes extending this base class, which make reference and changes to these private variables through functions of the base class. Here's an example, keep in mind I'm still confused about the difference between private and protected methods/variables (let me know if I'm doing it wrong!):

base.class.php

<?php
class Base {
    private $test;
    public function __construct(){
        require('sub.class.php');
        $sub = new Sub;
        echo($this->getTest());
    }
    public function getTest(){
        return $this->test;
    }
    protected function setTest($value){
        $this->test = $value;
    }
}
?>

sub.class.php

<?php
class Sub extends Base {
    public function __construct(){
        parent::setTest('hello!');
    }
}
?>

So I'd expect the result to be hello! printed on the screen - instead there is nothing. There could be a fundamental misunderstanding of classes on my part, or maybe I'm just doing something wrong. Any guidance is very much appreciated! Thanks.

EDIT:

Thank you to everyone who contributed an answer - I think, despite the excellent solutions, that child classes are actually not what I need - it seems delegate classes may be more useful at this point, as I don't really need to reference the Base functions from within the other classes.

4
  • 1
    Try PHP5 OOP. Commented May 31, 2012 at 3:37
  • 1
    There is no any echo or print to output something on the screen Commented May 31, 2012 at 3:38
  • @bumperbx - That worked... But that that may be impractical for my purposes. Commented May 31, 2012 at 3:46
  • So have you met php extension runkit, it allows you to change anything/any property regardless of it being private, at runtime Commented Mar 5, 2018 at 16:58

1 Answer 1

7

Should be like this:

base.class.php:

class Base {
    private $test;
    public function __construct() {
        echo $this->getTest();
    }
    public function getTest() {
        return $this->test;
    }
    protected function setTest($value) {
        $this->test = $value;
    }
}

sub.class.php:

class Sub extends Base {
    public function __construct() {
        parent::setTest('hello!');  // Or, $this->setTest('hello!');
        parent::__construct();
    }
}

main code:

require 'base.class.php';
require 'sub.class.php';

$sub = new Sub;  // Will print: hello!
Sign up to request clarification or add additional context in comments.

5 Comments

I would also replace parent:: with $this->
Ok, I can see why this works as opposed to mine I think. Is there any way I can reference the setTest function of the parent from the child function, so that a the code $this->test = $value sets the variable for the parent, as opposed to the child? I'd ultimately like to not have to make calls if I can make them from the child's constructor. I basically want to access the parent functions, not inherit them.
@iLoch I'm not quite understand with what you mean, but take a look at my edited code. Is it what you want?
Essentially, yes. I can understand how it may be complicated to understand - I have a feeling my interpretation is not how it should be done. Let me try to elaborate.. I have the base and the child. I want the child to manipulate the base function's variabless so that when it calls the setTest function, the reference to $this is referring to the base function, not the child which (in your example) has inherited that function from the base. Is this possible? So instead of inheriting a function, I want to simply use it so that references to $this are kept in the original scope (the base)
One way you may be able to think of it is if both the setTest and getTest functions, instead of using $this-> used Base:: so that the variables were always being written/read to/from the base class, and not the child classes.

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.