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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:49:54+00:00 2026-05-23T06:49:54+00:00

Okay, so the book says: when creating a new object, you can pass a

  • 0

Okay, so the book says: “when creating a new object, you can pass a list of arguments to the class being called. These are passed to a special method within the class, called the constructor, which initializes various properties.

I don’t know what “passing” is, really, especially not in this context. I also read that if you use a constructor method, it takes the name of the class. So, if you have


class User
{
  function __constructor($param1, $param2)
  {
    echo "User construct."
  }
}
user();

What is supposed to happen? I can’t tell because my pastebins say it’s a “possible CRF attack”, whatever that means….

Moving right along, we come to a later section in the book where code says:


$bothCatsObject = new tiger();
echo "Tigers have...
"; echo "Fur: " . $bothCatObject->fur . "
"; echo "Stripes: " . $bothCatObject->stripes; class cat { public $fur; // Cats have fur. function __construct() { $this->fur = "TRUE"; } } class tiger extends Cats { public $stripes; // Tigers have stripes. function __construct() { parent::__construct(); // Call parent construct first. $this->stripes = "True"; } }

Is the point of this second block of code to show that when an object is called, its constructs activate and set the properties in active memory or something? Or, perhaps is it to say that when a properties is called, if the property its value is set within the constructor, it will have a value. If its value isn’t set within a constructor (e.g., if it’s set within a non-constructor method), an error will be returned?

The book says ‘this code takes advantage of the benefits of constructor inheritance in the typical manner’. So, I’m hoping I’m not too far away. I’m wondering why constructors are useful. What are some applications where constructors are necessary, if I need to know that at this point in my study?

Finally, in this case, the “cats” class is never assigned an object. Because “tiger” extends the cats class, can the cats class be called via the tiger object? Here’s an example of what I mean:


$bothCatsObject = new tiger();
$bothCatsObject->catTest();  //Does this return an error?

class cat
{
  public $fur; // Cats have fur.
  function __construct()
  {
    $this->fur = "TRUE";
  }
  function catTest()
  {
    echo "The object works for cats and tigers."
  }
}

class tiger extends Cats
{
  public $stripes; // Tigers have stripes.
  function __construct()
  {
    parent::__construct();  // Call parent construct first.
    $this->stripes = "True";
  }
  function tigerTest()
  {
    echo "The object works for tigers and cats."
  }
}

Along these same lines, can I call the functions built into the “tiger” class from a new cat object because the tiger class is sibling to the cat class? In other words, would replacing lines 1 and 2 of the previous code block with this code work, or would it return an error:

$bothCatsObject = new cat();
$bothCatsObject->tigerTest();  //Does this return an error?

  • 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-23T06:49:54+00:00Added an answer on May 23, 2026 at 6:49 am

    First of all, run your script in your local machine.

    ok :

    class User
    {
      function __constructor($param1, $param2)
      {
        echo "My Name is {$param1} {$param2} ;)";
      }
    }
    

    when you initialize the user class, it will automatically run the __construct function:

    $user = new User("John","Smith");
    // this will output "My Name is John Smith ;)"
    

    Constructors run anytime you create a new instance of the class. an example is:
    Lets say you want to start a database connection and do some heavy work, you can write a a constructor that checks if there is already a db connection and if not create it and handles setting all the default configuations

    $database = new Database();
    // will automatically create a connection
    // ex: mysql_connect("username","password","....
    //     mysql_select_db(...
    // and so on
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay so i have a class called viewList. When the item it long pressed
Okay, I've seen but haven't programmed in C# before. You can assume I'm competent
Okay, as I learned via my previous question , the RWH book is already
Reading along with the agile book, it says this in a footnote: ... You
Okay I need some insight. I am taking a C++ class and am working
Okay, I know I'm doing something wrong - but can't figure out a better
Okay, so this seems simple, but I can't think of a straightforward solution; Basically
Okay, so I'm putting together a book store with Ruby on Rails. Books are
Okay, I've looked all over the internet for a good solution to get PHP
Okay, so I'm running a small test webserver on my private network. I've got

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.