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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:06:07+00:00 2026-05-19T05:06:07+00:00

Is there a way to instantiate a new PHP object in a similar manner

  • 0

Is there a way to instantiate a new PHP object in a similar manner to those in jQuery? I’m talking about assigning a variable number of arguments when creating the object. For example, I know I could do something like:

...
//in my Class
__contruct($name, $height, $eye_colour, $car, $password) {
...
}

$p1 = new person("bob", "5'9", "Blue", "toyota", "password");

But I’d like to set only some of them maybe. So something like:

$p1 = new person({
    name: "bob",
    eyes: "blue"});

Which is more along the lines of how it is done in jQuery and other frameworks. Is this built in to PHP? Is there a way to do it? Or a reason I should avoid it?

  • 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-19T05:06:07+00:00Added an answer on May 19, 2026 at 5:06 am

    the best method to do this is using an array:

    class Sample
    {
        private $first  = "default";
        private $second = "default";
        private $third  = "default";
    
        function __construct($params = array())
        {
             foreach($params as $key => $value)
             {
                  if(isset($this->$key))
                  {
                      $this->$key = $value; //Update
                  }
             }
        }
    }
    

    And then construct with an array

    $data = array(
         'first' => "hello"
         //Etc
    );
    $Object = new Sample($data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is similar to Can PHP instantiate an object from the name of the
I'm securing my MVC controllers with AuthorizeAttributes . Is there a way to instantiate
Is there way to set @include mixin(); to variable? I tried this @mixin bg-gradient($fallback,
I am trying to instantiate a PDO object like this: $this->pdo['pdo'] = new PDO('mysql:host=127.0.0.1;dbname=mydb;charset=UTF-8',
Is there a simple way to make a class object global for an entire
In PHP, if you try to instantiate a new SoapClient , and the WSDL
Duplicate: Is there a way to instantiate objects from a string holding their class
Is there any way to sanely instantiate WPF objects in LinqPad? Here's my example
In PHP, if you define a class, and then instantiate an object of that
Is there a way for me to instantiate the Spring MVC DispatcherServlet in code

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.