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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:45:48+00:00 2026-05-15T15:45:48+00:00

Sometimes it’s difficult to explain in human language what you want to do in

  • 0

Sometimes it’s difficult to explain in human language what you want to do in programming, but I will try…

Please explain to me, how can I implement the following logic. Suppose we have a template class:

$obj1=new Tmpl($somevar1, $somevar2, ...);
//we then add a new file to template
//as we don't have any files yet, new object won't created
$obj1->AddTmpl('file1.tmpl');
//we add a second file to template,
//it will be independent template
//but all properties from $obj1 must be available
$obj2=$obj1->AddTmpl('file2.tmpl');

$obj1->printTmplFile(); //should output file1.tmpl
$obj2->printTmplFile(); //should output file2.tmpl

$obj2->printInitialVars(); 
//will print $somevar1, $somevar2 constructed for $obj1;
//$obj1 of course must have these variables available also

So, the purpose of it is in creating new object for each new file of a template. Each new object should have all set of properties which have been established for old object. So, in this case, for example, we will not call a constructor each time with the same arguments. Also only $obj1 can create a copy of itself. And if it is first call to method AddTmpl, then we don’t create new copy.

  • 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-15T15:45:49+00:00Added an answer on May 15, 2026 at 3:45 pm

    (Here I assume that the AddTmpl function does not return a copy of the object itself.)

    The following line is wrong. You are saving the result of the AddTmpl function into $obj2, this does not return a copy of $obj1.

    $obj2=$obj1->AddTmpl('file2.tmpl');
    

    You have to use cloning like this:

    $obj2 = clone $obj1;
    $obj2->AddTmpl('file2.tmpl');
    

    Note that after the cloning, $obj2 and $obj1 are totally independant and any changes made to one will not be reflected to the other. This is the intended purpose!

    More information about cloning: http://php.net/manual/en/language.oop5.cloning.php

    Edit: fixed typo in code

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

Sidebar

Related Questions

Sometimes it's nice to start over. In C++ I can employ this following simple
Sometimes i face the following problem : string txt = con.Request.Params[Par_name].ToString();//the original par value
Sometimes, tomcat restart will cause my application stop immediately so that my consuming data
Sometimes I want to have temporary comments fully left justified on a line (//)
Sometimes I need the following pattern within a for loop. At times more than
Sometimes I want to run a maximum amount of IO actions in parallel at
Sometimes it's very difficult to find an error in an application because of static
sometimes we can from ClassA do : import com.software.ClassB; classbObject = new ClassB ;
Sometimes I forget to implement a method in one of my classes, and Xcode
Sometimes you have a function that will work for flat arguments. For example: send(player,message)

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.