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 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 I need to block access on a website, but I don't want to
Problem: Sometimes you will want to access a component from javascript with getElementById ,
Sometimes IE6 will render the text of a <ul> list the same color as
Sometimes a labeled break or continue can make code a lot more readable. OUTERLOOP:
Sometimes, in PL SQL you want to add a parameter to a Package, Function
Sometimes I get Oracle connection problems because I can't figure out which tnsnames.ora file
Sometimes it can be confusing reading code in instance members that refers to other
Sometimes with a complex header structure it happens some header is included, but it
Sometimes I need the following pattern within a for loop. At times more than
Sometimes when I'm editing page or control the .designer files stop being updated with

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.