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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:41:08+00:00 2026-05-17T18:41:08+00:00

I am getting a hang of OOP and finally started creating them in my

  • 0

I am getting a hang of OOP and finally started creating them in my scripts. One thing I don’t get it is the “$this” after creating an instance of class. For example, this guy coded:

class Form
{
protected $inputs = array();    

public function addInput($type, $name)
{
     $this->inputs[] = array("type" => $type,
            "name" => $name);
}


}

 $form = new form();

 $this->addInput("text", "username");
 $this->addInput("text", "password");

Notice that the last two lines show that he used $this->addInput().

How is it different from $form->addInput? I always used the name of variable I use to create an instance of class. I don’t see what $this->function() does. How does the PHP know which object it’s referring to?

From what I understand, $this->var is used in any methods within that object. If there’s no $this->var but rather plain $variable then it cannot be used in other methods other than the method that has that $variable, correct?

related: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me/3689613#3689613

  • 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-17T18:41:08+00:00Added an answer on May 17, 2026 at 6:41 pm
    // Incorrect
    $this->addInput("text", "username");
    $this->addInput("text", "password");
    

    This code is incorrect. There is no $this when you’re not in a class method. That should be $form. So to answer your question: the difference is that $form->addInput is correct and $this->addInput is invalid!

    // Correct
    $form->addInput("text", "username");
    $form->addInput("text", "password");
    

    It looks like you understand OOP better than the person who wrote this code. You’re drinking from a tainted well. Yikes!

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

Sidebar

Related Questions

I've finally started making a Rails app from scratch and I'm getting the hang
I am getting the hang of the OOP paradigm, and the art of making
I'm slowly getting the hang of C# and this question is probably a result
Please bear with me on this as I'm still getting the hang on Java.
I've just started learning Python and I'm slowly getting the hang of it, but
I'm playing around with Box2D for fun right now, and after getting the hang
I'm just getting the hang of OOP and have been playing around with Java
I started getting the hang of inheritance in Perl/Moose, but I'm starting to run
I'm having a hard time getting the hang of this. Say I have: NSMutableArray
Sorry folks...first post here. Just getting the hang of this site. Thanks for the

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.