Jump to content

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();
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.