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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:50:34+00:00 2026-05-16T15:50:34+00:00

How come a simple instantiation doesn’t work? I have been doing the same method

  • 0

How come a simple instantiation doesn’t work? I have been doing the same method to all of the classes I created/instantiated but this is the only one that’s giving me this kind of error.

 Fatal error: Call to a member function validate_fname_and_lname() on a non-object in /homepages/......../Validate.php on line 23

Here’s my code:

//Class Validate
<?php

require_once 'RegExp.php';

$myRegExp = new RegExp();

class Validate 
{
    //Sends each entry to corresponding RegExp function with appropriate regular expression
    function validate_form($un, $fname)
    {
        $err_counter = 0;

        if(!$this->myRegExp->validate_fname_and_lname($fname))
        {
            $error_identifier .= 'firstName+';
            ++$err_counter;
        }
    }
}


//Class RegExp
<?php

class RegExp 
{
    function validate_fname_and_lname($flname)
    {
        return preg_match("/[a-zA-Z' ']{2,}/", $flname);
    }
}
  • 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-16T15:50:34+00:00Added an answer on May 16, 2026 at 3:50 pm

    I think you are trying to access the global $myRegExp from within object scope.

    You should probaby add a constructor to your validator:

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

    And then instantiate your Validator like this:
    $validator = new Validate($myRegExp);

    And you should declare a member variable ‘myRegExp’ in your Validate class.


    And on a side note: I think you should rethink your design. If I were you I’d create an interface:

    interface IValidator
    {
        public function valid($input);
    }
    

    Let your specific regex classes implement that interface:

    class ValidateFnameAndLname implements IValidator
    {
        function valid($flname)
        {
            return preg_match("/[a-zA-Z' ']{2,}/", $flname);
        }
    }
    

    And construct your Validate class like this:

    class Validate 
    {
        protected $myRegExp;
        public function __construct(IValidator $validator)
        {
            $this->myRegExp = $validator;
        }
        //Sends each entry to corresponding RegExp function with appropriate regular expression
        function validate_form($un, $fname)
        {
            $err_counter = 0;
    
            if(!$this->myRegExp->valid($fname))
            {
                $error_identifier .= 'firstName+';
                ++$err_counter;
            }
        }
    }
    

    Then you are on your way to get a more coherent design.

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

Sidebar

Related Questions

This may be very simple but I have not been able to come up
Okay this may be a simple question but I have yet to come with
I'm just learning nHibernate and have come across what probably is a simple issue
The task is pretty simple, but I've not been able to come up with
Come see the amazing disappearing rectangle! But seriously I have a really simple HTML5
I've come to bother you all with another probably really simple C question. Using
I've come across a really strange problem. I have written a simple Deck class
I have a simple problem, but can't come with a simple solution :) Let's
I have recently come across a very simple Typed DataTable (without using a .XSD)(I've
I have a simple jQuery question. How come I can't do this? $(<div class='flora'></div>).insertBefore('</body>');

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.