krazymatty krazymatty - 7 months ago 9
PHP Question

How do I Instantiate a class within a class in .php

I'm new to OOP and I can't figure out why this isn't working. Is it not ok to instantiate a class with in a class. I've tried this with included file with in the method and it didn't make a change.

include('Activate.php');

class First {
function __construct() {
$this->activate();
}
private function activate() {
$go = new Activate('Approved');
}
}

$run = new First();

Answer

Okay I figured out my issue. My code had two errors. One that being my method was set to private and secondly I had an error in my $parms that I was sending to the class.

include('Activate.php');

class First {
    function __construct() {
    $this->activate();
    }       
    private function activate() {
    $go = new Activate('Approved');
    }
}

$run = new First();