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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:40:13+00:00 2026-05-20T06:40:13+00:00

Introductory OOP question for you: Situation: I want an abstract class with a public

  • 0

Introductory OOP question for you:

Situation: I want an abstract class with a public setter and abstract validator, since each child will validate that property in a unique way.

Code:

abstract class Parent {
    public function setName($name) {
        if (validateName($name)) {
            $this->_name = $name;
        } else {
            // error msg
        }
    }

    abstract public function validateName($name);
}

class Child extends Parent {
    public function validateName($name) {
        // ensure name gits this child's requirements
    }
}

Question: is this a “legal” design approach? I figured since the setName method will be the same for every child class, it should be a public method in the parent class, but the validator should be abstract to force child classes to implement it.

I’m just sketching this out on paper conceptually…

  • 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-20T06:40:14+00:00Added an answer on May 20, 2026 at 6:40 am

    Yes, that’s a valid approach, although your syntax in imperfect:

            if (validateName($name)) {
    

    should be this:

            if ($this->validateName($name)) {
    

    But if this is the scope of the architecture, why not just define abstract setName() that validates and sets it? Like this:

    interface Parent {
        public function setName($name);
    }
    
    class Child implements Parent {
        public function setName($name) {
            if (/* $name is valid */) {
                $this->_name = $name;
            } else {
                // error message
            }
        }
    }
    

    Of course I use an interface here, because there’s no code in Parent.

    But please realize, this depends on how you’re program is structured. I’d have to see at least a diagram of the program’s architecture to say definitely either way.

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

Sidebar

Related Questions

I'm in an introductory comp-sci class (after doing web programming for years) and became
I'm writing a console-environment Hangman game for my introductory programming class. The player chooses
I'm working on some introductory recursion problems and I have a clarifying question I'd
I have to do a small introductory presentation for my WCF Intro class (for
Could anyone recommend me a clear introductory article on the inner workings of each
I'm currently in an introductory level Java class, and am working on the classic
I'm working as a TA in an introductory programming class, and the students tend
This is a general logic question, common to most introductory language and machines courses.
Currently I am writing a program for an introductory Java class. I have two
I'm teaching an introductory class to programming and GUI development using Python, and have

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.