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 8410225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:07:20+00:00 2026-06-10T00:07:20+00:00

I wanna replace a variable in class A by calling function replace in class

  • 0

I wanna replace a variable in class A by calling function replace in class B. e.g. in code below I want replace ‘hi’ for ‘hello’ but output is ‘hi’
P.S : class B is some controller and must get instance in class A.
i’m using php 5.2.9+

<?php
$a = new A();
class A {
    protected $myvar;

    function __construct() {
        $this->myvar = "hi";
        $B = new B();
        echo $this->myvar; // expected value = 'hello', output = 'hi'
    }

    function replace($search, $replace) {
        $this->myvar = str_replace($search, $replace, $this->myvar);
    }
}

class B extends A {
    function __construct() {
        parent::replace('hi', 'hello');
    }
}
?>
  • 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-06-10T00:07:21+00:00Added an answer on June 10, 2026 at 12:07 am

    That’s not how classes and inheritance work.

    <?php
    $a = new A();
    class A {
        protected $myvar;
    
        function __construct() {
            $this->myvar = "hi";
    
            /**
             * This line declares a NEW "B" object with its scope within the __construct() function
             **/
            $B = new B();
    
            // $this->myvar still refers to the myvar value of class A ($this)
            // if you want "hello" output, you should call echo $B->myvar;
            echo $this->myvar; // expected value = 'hello', output = 'hi'
        }
    
        function replace($search, $replace) {
            $this->myvar = str_replace($search, $replace, $this->myvar);
        }
    }
    
    class B extends A {
        function __construct() {
            parent::replace('hi', 'hello');
        }
    }
    ?>
    

    If you were to inspect $B, its myvar value would be “hello”. Nothing in your code will modify the value of $a->myvar.

    If you want the declaration of $B to modify an A object’s member variables, you need to pass that object to the constructor:

    class A {
        .
        .
        .
    
        function __construct() {
             .
             .
             .
             $B = new B($this);
             .
             .
             .
        }
    }
    class B extends A {
        function __construct($parent) {
            $parent->replace('hi', 'hello');
        }
    }
    

    Note: This is a very poor implementation of inheritance; though it does what you “want” it to do, this is not how objects should interact with each other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna show some text (and images) in browser but this text shouldn't be
I wanna preg replace anything inbetween (THIS) those two bracket thingys I want to
I want to delete some certain characters that used wrongly in a string. ........I.wanna.delete.only.the.dots.outside.of.this.text...............
I wanna build a gallery, but the my problem is how to load the
i wanna do a single selection in my table view here is my code
i wanna creat a website for downloading some files (pdf,word,xls, such as these files),
I wanna get the string representation of a variable. For example, (def my-var {})
I wanna to detect an input tag that its name is: some[en] in jquery.
I wanna call some ASM functions in VB.NET. How can I do it? It's
I want to replace all double tokens in string with the double value appended

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.