Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6472665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:22:25+00:00 2026-05-25T06:22:25+00:00

How i can run method $this->ob->getVar() inside class B function C here ? I

  • 0

How i can run method $this->ob->getVar() inside class B function C here ? I get no. Did i must transfer string to constructor ?

<?php 


class A{

  public $tabb = array('1'=>'one', '2'=>'two');
  public $index;

  public function setVar($v){

    $this->index = $v;

  }

  public function getVar(){

    return $this->index;

  }

  public function arr(){

    return $this->tabb;

  }

}


class B{

  public $tab;

  public function __construct($var){

    $this->ob=new A;

    $this->tab = $var;

  }

    public function C(){

     return $this->D($this->tab, $this->ob->getVar());

      }

  public function D($l, $j){

    if(is_array($l) && isset($j)){

      print 'yes';

  } else { 

    print 'no';

  }

  }

}

$obb = new A;
$obb->setVar('onetwo');
$k = $obb->arr();
$obbb = new B($k);
$obbb->C();


?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:22:26+00:00Added an answer on May 25, 2026 at 6:22 am

    First, for the sake of convention your B class should declare a private variable of $obj, but that is not necessary in PHP.

    Second, your B class is just creating a new instance of A in its constructor. So you have two different A classes. The once inside B never has its index property populated.

    If you wanted to have the A object created outside the B object you’ll have to pass it in like this:

    $obbb = new B($k, $obb);
    

    So now your new B constructor is something like this:

    public function __construct($var, $someObject){
    
        if (!empty($someObject)) {
            $this->ob = $someObject;
        }
        else {
            $this->ob=new A;
        }
    
        $this->tab = $var;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i repeatedly run this method as long as the button is being
When trying to run this method I get an error: List that this enumerator
I want to know how I can run a method in a separate thread?
Can I rerender a jsf ui component when a valuechangelistener method is run? The
I can run this command in SqlManager to detach the db ALTER DATABASE mydb
OK - I know I can run my java process like this : java
I'm trying to run a method when the enter key has been pressed. This
I have an issue here where I'm trying to call a class method on
I want to put this method into background worker class, i am trying but
I'm trying to figure out how I can run unit tests on a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.