0

I'm really new to PHP classes and I was just wondering how to access functions within a PHP Class.

For example:

<?PHP
$cn = "myClass";
$myClass = new $cn;

class myClass
{
    function __construct()
    {
        doSomething(); // ?
    }
    private function doSomething() {
        echo "doSomething accessed!<br />";
    }
}
?>

How would I access doSomething() within the class? Any help would be much appreciated.

1
  • 1
    It seems to me that you're too lazy to spend at least few minutes browsing php manual where you could get it either from Example #1 Simple Class definition (with a bit of imagination) or take a look at first user comment whe you could find $this->existenceRequirement(); Commented Oct 4, 2012 at 10:40

1 Answer 1

5

You have to use $this:

<?PHP
$cn = "myClass";
$myClass = new $cn;

class myClass
{
    function __construct()
    {
        $this->doSomething(); // ?
    }
    private function doSomething() {
        echo "doSomething accessed!<br />";
    }
}
?>

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.