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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:33:07+00:00 2026-05-12T15:33:07+00:00

Hey there I’m wondering how this is done as when I try the following

  • 0

Hey there I’m wondering how this is done as when I try the following code inside a function of a class it produces some php error which I can’t catch

public $tasks;
$this->tasks = new tasks($this);
$this->tasks->test();

I don’t know why the initiation of the class requires $this as a parameter either :S

thanks

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();
  • 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-12T15:33:07+00:00Added an answer on May 12, 2026 at 3:33 pm

    You can’t declare the public $tasks inside your class’s method (function.) If you don’t need to use the tasks object outside of that method, you can just do:

    $tasks = new Tasks($this);
    $tasks->test();
    

    You only need to use the “$this->” when your using a variable that you want to be available throughout the class.

    Your two options:

    class Foo
    {
        public $tasks;
    
        function doStuff()
        {
            $this->tasks = new Tasks();
            $this->tasks->test();
        }
    
        function doSomethingElse()
        {
            // you'd have to check that the method above ran and instantiated this
            // and that $this->tasks is a tasks object
            $this->tasks->blah();
        }
    
    }
    

    or

    class Foo
    {
        function doStuff()
        {
            $tasks = new tasks();
            $tasks->test();
        }
    }
    

    with your code:

    class Admin
    {
        function validate()
        {
            // added this so it will execute
            $_SESSION['level'] = 7;
    
            if (! $_SESSION['level'] == 7) {
                // barMsg('YOU\'RE NOT ADMIN', 0);
                return FALSE;
            } else {
                $tasks = new Tasks();
                $tasks->test();
                $this->showPanel();
            }
        }
    
        function showPanel()
        {
            // added this for test
        }
    }
    class Tasks
    {
        function test()
        {
            echo 'test';
        }
    }
    $admin = new Admin();
    $admin->validate();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there, I have the following code: sVal = analogRead(potPin); // read the value
hey there i have this code that should save a file from sql server
hey there guys and girls i have this code that saves json as a
Hey there guys and gals. I'm very new to php and am following various
Hey there, assuming I have the following code in a mex routine: mxArray *newPoint
hey there, I have a Navigation Menu and my code looks like this.. <ul
Hey there I have this function: void vdCleanTheArray(int (*Gen_Clean)[25]) { } I wanted to
Hey there I've made a recursive permutation function for class, but I output is
Hey there, I am trying to get my signout button to work. This is
Hey there, was wondering if anyone could help a newbie on SQL and Python.

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.