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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:04:56+00:00 2026-05-14T01:04:56+00:00

myClass(123); ?> this works, but returns this warning: Warning: Missing argument 1 for myClass::myClass()

  • 0

myClass(123);

?>

this works, but returns this warning:

Warning: Missing argument 1 for myClass::myClass()

I read in to this, and seems that the constructor is expecting a value, so by adding:

function myClass($input='')

the warning is removed, but this seems so unnecessary?

could someone enlighten me as to why it’s required to define a value to prevent that warning?

thanks for any pointers

  • 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-14T01:04:57+00:00Added an answer on May 14, 2026 at 1:04 am

    You are using a method (functions in objects are called methods) that is the same name as the class. That is called the constructor, it has a special meaning in OOP.

    The constructor is never called separately, but is the method that gets called automatically when you initialize the object. Any parameters that method has, you append to the new classname statement.

    $test = new myClass(123);
    

    also, the constructor must never return a value. It is used only to do things while initializing the class, e.g. storing parameters. Any returned values will be lost, as the result of new myClass is always the initialized object.

    If you are just looking to create a method inside your class to return some text, then you need to change the method’s name. This would work:

    <?php
    class myClass {
     var $input;
     var $output;
    
     function someOtherFunctionName($input) {
      $output = 'You entered: ' . $input;
      return $output;
     }
    }
    
    $test = new myClass;
    echo $test->someOtherFunctionName(123);
    
    ?>
    

    If a constructor is indeed what you want to use, then note that since PHP 5, it is standard practice to use __construct() instead of creating a method with the same name as the class:

    <?php
    class myClass {
     var $input;
     var $output;
    
     function __construct($input) {
      $this->input = $input;  // This is a valid thing to do in a constructor
     }
    }
    

    More on constructors and destructors in PHP 5 in the manual.

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

Sidebar

Related Questions

I have a generic class public MyClass<TContext, T> where TContext : DataContext that effectively
Given this definition: [UniqueId(Ident)] public class MyClass : MyBase { public int Ident{ get;
I must be missing something stupid here but I can not see it. My
Given a class such as class MyClass: text = hello number = 123 Is
class MyClass { public: ~MyClass() {} MyClass():x(0), y(0){} //default constructor MyClass(int X, int Y):x(X),
I have a class myclass that has a private member param_map class some_class {
Consider: class MyClass<T> where T : class { } In that case, the where
I have this code: MyClass object; .... some code here where object may or
MyClass[] array; List<MyClass> list; What are the scenarios when one is preferable over the
public class MyClass { public int Age; public int ID; } public void MyMethod()

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.