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

  • Home
  • SEARCH
  • 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 8582045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:11:42+00:00 2026-06-11T21:11:42+00:00

Imagine two classes which share almost the same exact methods and properties, both extending

  • 0

Imagine two classes which share almost the same exact methods and properties, both extending a parent class, but the differences are minimal.

class fields { 
 public function __construct() {
  global $id;
  $this->id = $id++;
 }
}

class input extends fields {
 public function __construct() {
  parent::__construct();
 }
 public function draw() {
  echo '<input>';
 }
}

class textarea extends fields {
 public function __construct() {
  parent::__construct();
 }
 public function draw() {
  echo '<textarea>';
 }
}

I’m thinking it would be more efficient to rewrite the textarea class in this psuedo-code fashion:

class textarea extends fields {
 public function __construct() {
  $this = new input(); // <<------ 
 }
 public function draw() {
  echo '<textarea>';
 }
}

Basically, I’m unsure how this would best be done so that the class acts like the class from the first example.

In essence, I would like to do the following using OOP, but be able to use the object as it can be in the first example above (be able to call the possibly overloaded methods, have different properties, etc.):

function a() {echo '123';}
function b() {a();}

I have just copied the entire class and modify a few lines, but I feel it is wasteful.

Final Answer
Thanks to those people, here is the combined answer with example calls:

abstract class fields { 
 private static $masterid = 0;
 public function __construct() {
  $this->id = self::$masterid++;
 }
}

class input extends fields {
 public $data;
 public function __construct($new = '') {
  parent::__construct();
  if ($new) $this->data = $new;
  else $this->data = 'Hello';
 }
 public function draw() {
  echo '<input>'.$this->export().'</input>';
 }
 public function export() {
  return 'ID '.$this->id.' = '.$this->data;
 }
}

class textarea extends input {
 public function __construct($new = '') {
  parent::__construct($new);
 }
 public function draw() {
  echo '<textarea>'.$this->export().'</textarea>';
 }
}

$a = new textarea();
$a->draw();
$a = new textarea('World');
$a->draw();
$a = new input('!');
$a->draw();

//Outputs:
// <textarea>ID 0 = Hello</textarea>
// <textarea>ID 1 = World</textarea>
// <input>ID 2 = !</input>
  • 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-11T21:11:44+00:00Added an answer on June 11, 2026 at 9:11 pm

    Make the fields class an abstract class, and like Darren suggested, make the ‘draw’ method a function of the fields class.

    Now heres the trick, you want the input class to extend fields, but override the draw method. This will allow you to customize the functionality of that method, and you can still call the parent variation from within it.

    Finally, since the textarea class is going to have many similarities to the input class, make textarea extend input. Thereby inheriting the properties and methods of both fields and input.

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

Sidebar

Related Questions

Imagine two simple java applications. Both of them are implementing the same JAR file
Imagine the following two classes of a chess game: TChessBoard = class private FBoard
Imagine having a main class -- Simulator -- that uses two other classes --
Imagine these two classes: class Part { public string Name { get; set;} public
Imagine I have two classes, class1 and class2. Class1 inherits from baseClass, which looks
I imagine two, but I would like to confirm if I am making sense
Imagine two projects. The first is the framework-core project which is in version 1.1.0
Let's assume I have two service classes with the following methods: GroupService createGroup() deleteGroup()
Imagine in C++ two classes one named derived and another named base that is
I have two classes: class Activity < ActiveRecord::Base belongs_to :activity_type def belongs_to_cat_a? self.activity_category ==

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.