Chris Fletcher Chris Fletcher - 1 year ago 95
PHP Question

Can't seem to access an object's properties

I have a simple function:

function myFunction($this){

Which outputs this:

object(MyClass)#810 (20) {
// ...more properties

However, if I try to access "course_id" on the object, nothing gets output:


In the class definition,
is public:

class MyClass extends AnotherClass {

public $course_id = 0;

What am I doing wrong? Thank you :)

Answer Source

It's because you're using the name $this which is a pseudo-variable that becomes available when a method is called from within an object context, but you're not calling it within an object context, you're calling it from within a function context.

Change the parameter name and it works:

class MyClass {
    public $course_id = 0;

function myFunction($temp) {

$test = new MyClass();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download