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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:04:52+00:00 2026-05-16T15:04:52+00:00

<?php class test{ function foo(){ echo $this->abc; } } $test = new test; $test->abc

  • 0
<?php
    class test{
        function foo(){
            echo $this->abc;
        }
    }
    $test = new test;
    $test->abc = 'abc';
?>

Remeber that i don’t declare the variable abc, but i want to set $abc to ‘abc’. How to do this
Sorry because this is a dummy question

  • 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-16T15:04:53+00:00Added an answer on May 16, 2026 at 3:04 pm

    Actually, this is already working. But it is a bad practice.

    Normally you should do something like this:

    class test {
        private $abc;
    
        public function foo() {
            echo $this->abc;
        }
    
        public function getABC() {
            return $this->abc;
        }
    
        public function setABC($abc) {
            // You can also add some additionally checks
            $this->abc = $abc;
        }
    }
    

    Which makes:

    $bar = new test;
    $bar->setABC('abc');
    $bar->foo();
    

    Remember to make your class attributes ($abc) always private or protected. It’s encapsulation that makes OOP so powerfull.

    If you want to use dynamic variable names, so instead of $this->abc you want to set $this->cba you should use the magic methods __set and __get

    More about magic methods can be found here:

    http://php.net/manual/en/language.oop5.magic.php
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following classes: <?php class test{ static public function statfunc() { echo this
Why is this legal in PHP? <?php class Foo { public function test() {
there are some code as this: <div class=test> <div class=upsell-tags> <?php echo $this->getChildHtml('product_additional_data') ?>
Quick question regarding __callStatic() in PHP; class Test{ public static function __callStatic($method, $arguments){ echo
I don't understand why doesn't this code work: <?php class Test { private $BIG
i'm testing php's reflection with default params like this... class Test{ public function testMethod($class,$methodName){
<?php abstract class a{ abstract protected function test(); function threeDots(){ return '...'; } }
<?php class Conversor { function toLowerFirst($word) { $word = 'test'; return $word; } }
is it possible to do something like this in PHP 5.2.1? abstract class Test
This does work: $test = new Test(); $test->blah(); class Test // extends DateInterval {

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.