Jump to content

object orientation question


purencool

Recommended Posts

I have instantiated a object and the constructor has an other object added the issue arises when I  extent the first object how do I access the object in the variable. below is a stripped down example. I want accessfoo to access class a method foo that is stored in a var in class b is this possible.

class a {
private function foo(){
return "hello word";
}
}


$a = new a()


class b {
    private $obj;
    public function __construct ($obj){

        $this->obj= $obj;

    }

}

class c extends b{

private function accessfoo(){
    $this->obj ?????????????
}

$c = new class($a)

Link to comment
https://forums.phpfreaks.com/topic/234315-object-orientation-question/
Share on other sites

I suggest reading the following examples:

http://php.net/manual/en/language.oop5.visibility.php

 

Here are the correct access levels for your class methods:

<?php
class a {
public function foo() {
	print "hello word";
}
}

class b {
    protected $obj;
    public function __construct($obj) {
        $this->obj = $obj;
    }
}

class c extends b {
public function accessfoo(){
    	$this->obj->foo();
}
}

$a = new a();
$c = new c($a);
$c->accessfoo();
?>

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.