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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:49:30+00:00 2026-05-27T10:49:30+00:00

I am writing a validation class in PHP which I would like to be

  • 0

I am writing a validation class in PHP which I would like to be extendible without editing the main parent class. I have provided a simplified version below of what I hope to achieve. I am passing a function/method name to validate() which first checks if it exists and if it does invokes it to check the variable I passed is valid. I am new to OOP and having problems with scope / visibility as I’m unable to get any custom validation rules in the child class working without hardcoding the name of the child class in the parent class. What is the best way to go about this? Many thanks for any assistance you can provide.

$rule = "number";
$var = "abcdef";

class  Validation
{
    public static function validate($rule, $var) {
        if (is_callable("self::{$rule}")) {
            return self::$rule($var);
        }
    }

    protected static function number($var) {
        return (preg_match("/^[0-9]+$/i", $var));
    }
}

class MyRules extends Validation
{   
    public static function letter($var) {       
        return (preg_match("/^[a-zA-Z]+$/i", $var));
    }
}

print MyRules::validate($rule, $var) ? "Valid!" : "Not valid!"; // Not valid!
  • 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-27T10:49:31+00:00Added an answer on May 27, 2026 at 10:49 am

    Firstly, you can prevent overriding of the validate method using the final keyword:

    public static final function validate($rule, $var) {
    

    As for not being able to call static methods of subclasses, this can be done using Late Static Binding:

    class Validation {
        public static final function validate($rule, $var) {
            if (is_callable("static::$rule")) {
                return static::$rule($var);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a php forms class with client and server side validation. I'm having
I am currently writing some unit tests for a business-logic class that includes validation
I have a strange problem with the validation I am writing on a form.
Writing my first PHP Class and came across an issue, the following is my
I'm writing this in PHP, but, generally speaking, what methods and properties would a
I am writing a custom configuration section, and I would like to validate a
Hi I'm writing and app in PHP. I want to use jquery form validation
I have around 20 controls which are binded to different properties of a class
I am writing a form validation class and wish to include regular expressions in
I'll be tackling writing a custom date validation class tomorrow for a meeting app

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.