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

The Archive Base Latest Questions

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

See the following example (PHP) class Parent { protected $_property; protected $_anotherP; public function

  • 0

See the following example (PHP)

class Parent
{ 
  protected $_property;
  protected $_anotherP;

  public function __construct($var)
  {
    $this->_property = $var;
    $this->someMethod();  #Sets $_anotherP
  }

  protected function someMethod()
  ...
}

class Child extends Parent
{
  protected $parent;

  public function __construct($parent)
  {
    $this->parent = $parent;
  }

  private function myMethod()
  {
    return $this->parent->_anotherP;  #Note this line
  }
}

I am new to OOP and am a bit ignorant.

Here to access the parents property I am using an instance of that class, which seems wrong :S (no need of being i child then). Is there an easy way, so that i can sync the parent properties with the child properties and can directly access $this->anotherP without having to use $this->parent->anotherP ?

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

    As your Child class is extending your Parent class, every properties and methods that are either public or protected in the Parent class will be seen by the Child class as if they were defined in the Child class — and the other way arround.

    When the Child class extends the Parent class, it can be seen as “Child is a Parent“ — which means the Child has the properties of the Parent, unless it redefines those another way.

    (BTW, note that “parent” is a reserved keyword, in PHP — which means you can’t name a class with that name)

    Here’s a quick example of a “parent” class :

    class MyParent {
        protected $data;
        public function __construct() {
            $this->someMethodInTheParentClass();
        }
        protected function someMethodInTheParentClass() {
            $this->data = 123456;
        }
    }
    

    And it’s “child” class :

    class Child extends MyParent {
        public function __construct() {
            parent::__construct();
        }
        public function getData() {
            return $this->data; // will return the $data property 
                                // that's defined in the MyParent class
        }
    }
    

    That can be used this way :

    $a = new Child();
    var_dump($a->getData());
    

    And you’ll get as output :

    int 123456
    

    Which means the $data property, defined in the MyParent class, and initialized in a method of that same MyParent class, is accessible by the Child class as if it were its own.

    To make things simple : as the Child “is a” MyParent, it doesn’t need to keep a pointer to… itself 😉

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

Sidebar

Related Questions

See the following code of the <?php class DefaultController extends Controller { public function
See following code with resides in fillowing directory mypack.pack1 package mypack.pack1; public class myclass
See the following Mock Test by using Spring/Spring-MVC public class OrderTest { // SimpleFormController
I am wondering if PHP will re-use the Parent class. With the following code:
I have a question on the stream behavior, see the following example. What I
Please see following methods. public static ProductsCollection GetDummyData(int? customerId, int? supplierId) { try {
See the following: struct A { std::string* get() const { //return const_cast<std::string*>(&m_pObj); return &const_cast<A*>(this)->m_pObj;
I have the following PHP function which will list the users from a MySql
<?php echo(var answersC = answer-table-$qrow->id;); echo(var toggleC = toggle-table-$qrow->id;); ?> jQuery('toggleC').click(function() { jQuery('answersC').slideToggle('fast'); });
As per here I've got the following controller: class User extends CI_Controller { 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.