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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:06:05+00:00 2026-06-01T04:06:05+00:00

I currently have a function where I’m trying to refer to the $id in

  • 0

I currently have a function where I’m trying to refer to the $id in the class but it doesn’t work:

public function getCourseInfo($cid = $this->id, $all = false)
{

}

This is my class:

class Course
{
    protected $course;
    protected $id;

    public function __construct($cid)
    {
        $id = $cid;
        $this->course = $this->getCourseInfo($this->id);
    }
    public function getCourseInfo($cid = $this->id, $all = false)
    {

    }
}
  • 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-06-01T04:06:07+00:00Added an answer on June 1, 2026 at 4:06 am

    Everyone in this thread is giving correct answers but no one gave a full code sample hence I’ll post my suggestion:

    class Course
    {
    
        /**
         * @var int
         */
        protected $_courseId;
    
        /**
         * @var array
         */
        protected $_course;
    
        /**
         * Class constructor
         *
         * @param int $courseId Course ID
         * @return Course
         */
        public function __construct($courseId)
        {
            $this->_courseId = (int) $courseId;
        }
    
        /**
         * Get course information
         *
         * @param bool $all ...
         * @return array
         */
        public function getCourseInfo($all = false)
        {
            if ($this->_course === null) {
                // use $this->_courseId as needed
                $this->_course = ... // populate course info
            }
    
            return $this->_course;
        }
    
    }
    

    As you’ll notice I’ve omitted the course id parameter from getCourseInfo() simply because it’s not needed if you instantiate the class with a course id.

    Secondly, I don’t think you should call getCourseInfo in the constructor because the information will only be needed at a later point. Also, I added “caching” to the function so that you don’t fetch data twice.

    Obviously there’s a high chance that I could be wrong having not seen your code but I feel this is a better structure of the code.

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

Sidebar

Related Questions

How would I create a nested list, I currently have this public function getNav($cat,$subcat){
I currently have a function that looks like this: public void AnimateLayoutTransform(object ControlToAnimate) {
I currently have this method: public function getUser($id) { $user = $this->db->getSingleRecord(SELECT user_id, user_login,
I currently have a function: public static Attribute GetAttribute(MemberInfo Member, Type AttributeType) { Object[]
Currently i have this: $(.splitCol).click(function () { $.cookie('whichColumn', 'split'); $(.threeCol .active).removeClass(active); $(.leftCol .active).removeClass(active); $(.splitCol
I am currently trying to build a program where I have a recursive function
I currently have this: function SystemCollection () { this.systems = []; this.getSystem = function
I currently have this in my document $(document).ready(function(){ $([href$='.html']).addClass('html'); $([href$='.pdf']).addClass('pdf'); }); which styles any
I currently have this function which appends an input to the #cursor-start div: var
I currently have a function that grabs the browser windows uri, parses out the

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.