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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:18:36+00:00 2026-06-07T06:18:36+00:00

ok i have 2 classes class A{ public function say(){ echo hello<br>; } }

  • 0

ok i have 2 classes

class A{  
     public function say(){
        echo "hello<br>";
     }  
  }

 class B extends A{
    public function say(){
        echo "hi<br>";
    }
 }

 $haha = new B();
 $haha->say();

well as you see i overloaded the method say() in class b… but what i want here is to merge the two methods and not overwrite each other. my desired output I want is this

hello<br>
hi<br>

is this possible?

NOTE: if my terms are wrong, please teach me the right terms. I am really new to PHP OOP

  • 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-07T06:18:39+00:00Added an answer on June 7, 2026 at 6:18 am

    Edit:
    Based on your comments, you want something like this:

    class A{
        final public function say(){
            echo "hello<br>";
            $this->_say();
        }
        
        public function _say(){
            //By default, do nothing
        }
    }
    
    class B extends A{
        public function _say(){
            echo "Hi<br>";
        }
    }
    

    I have called the new function _say, but you can give it any name you want. This way, your teammates just define a method called _say(), and it will automatically be called by class A‘s say method.

    This is called the Template method pattern

    Old answer

    Just add parent::say(); to the overloading method:

    class B extends A{
        public function say(){
            parent::say();
            echo "hi<br>";
        }
     }
    

    This tells php to execute the overloaded method. If you don’t want extending classes to overload it’s methods, you can declare them final.

    See also the php manual about extending classes.

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

Sidebar

Related Questions

Lets say I have these classes: class Foo { public $_data; public function addObject($obj)
Let's say I have two classes: class A { public: A* Hello() { return
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Say I have three classes: class X{}; class Y{}; class Both : public X,
Let's say I have two classes that look like this: public class ByteFilter {
Say I have several ObservableCollections of different classes: public class Employee { public int
I have two classes class A { public string something { get; set; }
my code is like this: i have two classes first class: public class Box<E>
I have two classes: public class MultilingualString { public int Id { get; set;
I have a 3 classes: Class 1: public IEnumerable<double> Values; public Class2 class2Instance; Class2:

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.