• 0

[PHP] Help with classes


Question

test.php

<?php

class test_a
{
	static public function run()
	{
		$test = new test_a();

		include("test2.php");
		$test->test_b = new test_b();

		$test->test_b->testfunction();
	}

	public function abc()
	{
		return 'Testing 123';
	}
}

test_a::run();

?>

test2.php

<?php

class test_b
{
	public function testfunction()
	{
		return $this->abc();
	}
}

?>

How can I execute the abc() function in test2.php when the abc() function is in the test_a class in test.php?

Edited by MNunes2
Link to comment
Share on other sites

14 answers to this question

Recommended Posts

  • 0

Maybe I missed a bit, but have you created "instances" e.g., $testRunA = new test_a ();. If you have then you can run the functions inside the class by calling the functions via the instance of the class you created earlier. So that would code out like:

$testRunA->abc(); which should return Testing 123 on the screen or if your parsing it somewhere else etc.

To execute it in another PHP page, you must include or require_once the test.php page within the PHP page you would like to call the functions in so that it can see the classes etc.

Link to comment
Share on other sites

  • 0

I want to run functions from the class test_a in the class test_b. The class test_b is included inside the run() function so to run a function from class_b in class_a would be $this->test_b->testfunction().

The above example returns this error:

Fatal error: Call to undefined method test_b::abc() in C:\wwroot\test2.php on line 7

Edit:

Found the solution:

<?php

class test_b
{
	public function testfunction()
	{
		return test_a::abc();
	}
}

?>

Edited by MNunes2
Link to comment
Share on other sites

  • 0

Two solutions, one, make your child class inherit from the parent and use parent::FunctionName()

or,

	class ParentClass
	{
		function CallChild()
		{
			$objChild = new ChildClass($this);
			$objChild->CallParent();
		}

		function DisplayMessage()
		{
			echo "Hello from the Parent from the Child from the Parent";
		}
	}

	class ChildClass
	{
		var $parent;
		function __construct($objParent)
		{
			$this->parent = $objParent;

		}

		function CallParent()
		{
			$this->parent->DisplayMessage();
		}

	}

	$Var = new ParentClass();
	$Var->CallChild();

Edit: Hmm, I'm not to sure what will happen making the function static. When it calls a static function is it going to refer to the last object created or will it just treat it as a new object (unless everything else is defined as static)

Link to comment
Share on other sites

  • 0

Yes, the error will be generated because the abc() function you are asking for is not in the scope (in plain english it cannot the see the function you are asking to run). If you want to merge functions, I would make an instance in the include file and tie that to a global variable.

Then you can use that variable which will point to the object it refers to anywhere within test_a class.

It is also confusing to declare a new instance of a class inside the function of another object unless you are extending a class. This might need a rewrite. If a get a spare few, I will draft an example up.

Yeah, you got it, directly ask to run the function from the other class.

It's funny, when it comes to OO programming, there seems to be a few ways to doing the same thing. That's probably what makes it more confusing at times ;)

Link to comment
Share on other sites

  • 0

This is a possible solution depending on your scenario:

//rewrite
testA.php

class test_A {

	public function abc()
	{
		return 'Testing 123';
	}
}


testB.php

include ("testA.php");
$parentLink = new test_A();


class test_B {
	public function testfunction()
	{
		global $parentLink;
		return $parentLink->abc();
	}
}

//then in my main PHP page including the object php files
//I would type the following:

include ("testB.php");

#new instance of testB class which automatically will pull in testA class
$runThis = new test_B();
$runThis->testfunction(); //displays 'Testing 123'

Link to comment
Share on other sites

  • 0
This is a possible solution depending on your scenario:

//rewrite
testA.php

class test_A {

	public function abc()
	{
		return 'Testing 123';
	}
}


testB.php

include ("testA.php");
$parentLink = new test_A();


class test_B {
	public function testfunction()
	{
		global $parentLink;
		return $parentLink->abc();
	}
}

//then in my main PHP page including the object php files
//I would type the following:

include ("testB.php");

#new instance of testB class which automatically will pull in testA class
$runThis = new test_B();
$runThis->testfunction(); //displays 'Testing 123'

You want to get rid of the global in there. That creates an ugly hidden dependency. Just feed the dependency (test_a) into the constructor of test_b.

Link to comment
Share on other sites

  • 0

Hehe, yes you are right :). Why an earth did I write it that way. Good we learn new things everyday. ;)

Link to comment
Share on other sites

  • 0

Hehe, yeah practically is. It's been a while since I've had to do any mainstream OO programming so it's always good to see the code from another point of view. :)

Link to comment
Share on other sites

  • 0

I'm guessing it's thinking like that, that got you into this mess. ;)

It's all about context really, unless of course this is merely exploratory.

<?php
abstract class Book
{
	protected $category = 'unknown';

	public function getCategory()
	{
		return $this->category;
	}
}

class ProgrammingBook extends Book
{
	public function __construct()
	{
		$this->category = 'Programming';
	}
}

$oPHP5 = new ProgrammingBook();

echo $oPHP5->getCategory(); #Programming
?>

Link to comment
Share on other sites

  • 0
Why so much code? Just call a function from another class like this: "anotherclass::function();" :D Works perfectly.

i think that will only work if 'function()' is a public static method of 'anotherclass'.. that is without using inheritance.

Link to comment
Share on other sites

  • 0
I'm guessing it's thinking like that, that got you into this mess. ;)

It's all about context really, unless of course this is merely exploratory.

<?php
abstract class Book
{
	protected $category = 'unknown';

	public function getCategory()
	{
		return $this->category;
	}
}

class ProgrammingBook extends Book
{
	public function __construct()
	{
		$this->category = 'Programming';
	}
}

$oPHP5 = new ProgrammingBook();

echo $oPHP5->getCategory(); #Programming
?>

Thank God someone finally posted the proper way. Use the 'extends' keyword.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.