Jump to content

Call method in Class B from within Class A


rondog

Recommended Posts

I have two classes:

 

## Admin.php

<?php
class Admin
{
public function __construct()
{
	include("Config.php");
}

/**
* deletes a client
* @returns true or false
*/
function deleteClient($id)
{
	return mysql_query("DELETE FROM usernames WHERE id = '$id'");
}
}
?>

 

## Projects.php

<?php
class Projects
{
public function __construct()
{
	include("Config.php");
	$this->admin = $admin;
	$this->dataFolder = $dataFolder;
}

/**
* Deletes a project
* @returns true or false
*/
function deleteProject($id)
{
	$root 		= $_SERVER['DOCUMENT_ROOT'];
	$theDir 	= $root . $this->dataFolder;
	$sql 		= mysql_query("SELECT * FROM projectData WHERE proj_id = '$id'");
	while ($row = mysql_fetch_array($sql))
	{
		$mainFile = $row['path'];
		$thumb = $row['thumbnail'];
		if ($thumb != 'null')
		{
			unlink($theDir . "/" . substr($thumb,13));
		}
		unlink($theDir . "/" . substr($mainFile,13));
	}
	$delete = mysql_query("DELETE FROM projectData WHERE proj_id = '$id'");
	$getDir = mysql_query("SELECT proj_path FROM projects WHERE id = '$id'");
	$res = mysql_fetch_array($getDir);
	rmdir($theDir . "/" . $res['proj_path']);
	return mysql_query("DELETE FROM projects WHERE id = '$id'");
}
}
?>

 

How can I call deleteProject() from within Admin.php?

Like you would call any other class methods...

 

You could just include the Projects.php file and call it Projects::deleteProject()...

 

What i normally do is define a variable and assign the object to it on construction... then call it like: $this->var->method(). e.g.

Class something
{
  protected $_projects;
  public function __construct()
{
   require_once('Projects.php');
   $this->_projects = new Projects;
}
public somefunc()
{
  $this->_projects->method();
}
}

 

But then again i usually have similar classes on a same file.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.