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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:10:28+00:00 2026-05-30T09:10:28+00:00

How can i pass a class as a parameter in my function So far

  • 0

How can i pass a class as a parameter in my function

So far i’ve tried

$sc = new SampleClass();
SampleFunction($sc);


function SampleFunction(&$refClass)
{
    echo $refClass->getValue();
}

this is a simplified example of what im doing.. i actually have to do complex procedures inside this sample function. I’m not getting any response from the sample function. What am i doing wrong? thank you

UPDATE

char.php

   class Charss {
    var $name=0;
    var $hp=500;
    var $spd=10;
    var $rtime=10;
    var $dmg=10;

    function __construct( $name, $hp, $spd, $rtime , $dmg) { 
            $this->name = $name;
            $this->hp = $hp;
            $this->spd  = $spd;
            $this->rtime = $rtime;
            $this->dmg = $dmg;
        }

    function get_name() {
        return $this->name;
    }

    function set_name($new_name) {
        $this->name = $new_name;
    }

    function get_hp() {
        return $this->hp;
    }

    function set_hp($new_hp) {
        $this->hp = $new_hp;
    }

    function get_spd() {
        return $this->spd;
    }

    function set_spd($new_spd) {
        $this->spd = $new_spd;
    }

    function get_rtime() {
        return $this->rtime;
    }

    function set_rtime($new_rtime) {
        $this->rtime = $new_rtime;
    }

    function get_dmg() {
        return $this->get_dmg;
    }

    function set_dmg($new_dmg) {
        $this->dmg = $new_dmg;
    }
}

myclass.php

    require("char.php");
class Person {

function try_process()
{
    $chr1 = new Charss("Player1",500,3,0,50);
    $chr2 = new Charss("Player2",500,6,0,70);

    while ($chr1->get_hp() > 0 && $chr2->get_hp() > 0)
    {
        $sth = min($chr1->get_rtime(), $chr2->get_rtime());
        if ($chr1->get_rtime() == 0 && $chr2->get_rtime() > 0)
        {
            exit;
            Fight($chr1,$chr2);
            $chr1->set_rtime($chr1->get_spd());
        }
        elseif ($chr2->get_rtime() == 0 && $chr1->get_rtime() > 0)
        {
            Fight($chr2,$chr1);
            $chr2->set_rtime($chr2->get_spd());
        }
        else 
        {
            Fight($chr1,$chr2); #having trouble with this
            $chr1->set_rtime($chr1->get_spd());
        }
        $chr1->set_rtime($chr1->get_rtime() - $sth);
        $chr2->set_rtime($chr2->get_rtime() - $sth);
    }
}

function Fight($atk,$def)
{
    $def->set_hp($def->get_hp() - $atk->get_dmg());
    echo $atk->get_name() . " attacked " . $def->get_name() . " for " . $atk->get_dmg() . " damage";
}

}

so im calling the function try_process on button click

  • 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-30T09:10:29+00:00Added an answer on May 30, 2026 at 9:10 am

    What you’re actually doing there is passing an object, not a class.

    $sc = new SampleClass();
    

    creates an instance of SampleClass, aka an object.

    I assume there’s some error being thrown elsewhere as what you have is correct.
    I tested the following code and got the expected output:

    class SampleClass
    {
        public function getValue()
        {
            return 4;
        }
    }
    
    $sc = new SampleClass();
    SampleFunction($sc);
    
    function SampleFunction(&$refClass)
    {
        echo $refClass->getValue();
    }
    

    Output: 4

    If you provide more details of your actual code we might be able to determine the problem.

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

Sidebar

Related Questions

Can I pass this to a function as a pointer, from within the class
Mootools Ajax class has a onComplete parameter whereby you can pass a function name
How can I pass parameters to the OnSuccess function of the AjaxOptions class in
If I have a auto_ptr I can pass it for a reference?Like: auto_ptr<MyClass>Class(new MyClass);
In C++, you can pass into a function an object that is const. This
This is an extension question of PHP pass in $this to function outside class
You can pass special strings into jQuery's Datepicker class setDate() method like +7 which
What is the highest class in the hierarchy I can use to pass HttpSessionState
You can pass a function pointer, function object (or boost lambda) to std::sort to
I know you can pass arguments through the RunWorkerAsync function call when you first

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.