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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:13:19+00:00 2026-05-25T18:13:19+00:00

class Magic{ public $a=i; public $b=array(a=>A,b=>B,c=>C); public $c=array(1,2,3); public function __get($v) { echo <br/>get->$v;

  • 0
class Magic{
    public $a="i";
    public $b=array("a"=>"A","b"=>"B","c"=>"C");
    public $c=array(1,2,3);

    public function __get($v)
    {
     echo "<br/>get->$v";
     return $this->b[$v];
    }

    public function __set($var,$val)
    {
    echo "<br/>set->$var: $val,";
    $this->$var=$val;
    }
    }

    $m=new Magic();
    echo $m->a." , ".$m->b." ,".$m->c." ,";
    $m->c="CC";
    echo $m->a." , ".$m->b." ,".$m->c;

Output:i , Array ,Array ,i , Array ,CC

Now, I modify the visibility to protected for variable $b and $c.

Output: bci , B ,C ,c: CC,bci , B ,C

As far as I understand _get and_set is called for undefined var .So, when access modifier or visibility is set to protected why __get() is called !.

Can someone explain ….. I am not able to understand this.

  • 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-25T18:13:20+00:00Added an answer on May 25, 2026 at 6:13 pm

    From the manual

    __set() is run when writing data to inaccessible properties.

    __get() is utilized for reading data from inaccessible properties.

    A protected / private property is considered inaccessible from an external scope.

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

Sidebar

Related Questions

<?php abstract class AbstractClass { public function __get($theName) { return (isset($this->$theName)) ? $this->$theName :
Consider this class: class Test { public $obj; function __construct ($obj) { $this->obj =
I am trying to create a registry class with magic __set and __get my
So, i would like to implement something like this: class Collection{ private $array; public
For example this class: class A{ public function __call($func, $args){ if($func == 'something') call_user_func_array($this->_some_magic,
class Score { var $score; var $name; var $dept; var $date; function Score($score, $name,
class MyBase { protected object PropertyOfBase { get; set; } } class MyType :
I'm using the __get() and __set() magic methods in order to intercept external calls
Good afternoon all, I have a class which looks like this: public class Grapheme
I'm writing API implementation and my main API class has __call() magic method: public

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.