I would like to do something like:
library.php:
require_once "laucher.php";
class Test{
public function __construct(){
print "test";
}
}
class Foo extends Bar{
public function __construct(){
$t = new Test();
}
}
class Bar{
public function __construct(){
}
}
And in laucher.php, I would like to create a Foo object as $t = new Foo();
How Can I create Foo Objects in laucher.php? I would like to create an "auto-laucher" of Foo();
You have to include the file Foo is located in. So...
include("foo_file.php");
Then you can instantiate Foo.
$my_object = new Foo();
You cannot create Foo objects before Foo has been defined. Therefore you can't create Foo objects inside "laucher.php" if you include it before the class declarations.
However, if laucher.php is included after the class declarations you should be able to create Foo objects inside. So I think this would work:
class Test{
public function __construct(){
print "test";
}
}
class Foo extends Bar{
public function __construct(){
$t = new Test();
}
}
class Bar{
public function __construct(){
}
}
require_once "laucher.php";
Related
I'm new to programming. I have this going on:
I have Class A, which have many functions. One of those functions is functionX.
In functionX I need to make a call to functionY which belongs to another class: Class B.
So how do I acces to functionY from inside functionX?
I use Codeigniter.
Thanks in advance.
Try and experiment with this.
class ClassA {
public function functionX() {
$classB = new ClassB();
echo $classB->functionY();
}
}
class ClassB {
public function functionY() {
return "Stahp, no more OO, stahp!";
}
}
Class function? A static method?
If you have an instance (public) method, you just call $classB->functionY().
If you have a static method, you would call ClassB::functionY();
So:
class ClassA {
public function functionX(){
$classB = new ClassB();
// echo 'foo';
echo $classB->functionY();
// echo 'bar';
echo ClassB::functionYStatic();
}
}
class ClassB {
public $someVar;
public static $someVar2 = 'bar';
function __construct(){
$this->someVar = 'foo';
}
public function functionY(){
return $this->someVar;
}
public static function functionYStatic(){
return self::$someVar2;
}
}
Well that depends. If that function is a static function or not.
First off you must include the file with the class...
include_once('file_with_myclass.php');
If it is static you can call it like this:
ClassName::myFunction()
If it is not, then you create an instance of the class and then call the function on that instance.
$obj = new ClassName();
$obj->myFunction();
As you can guess the function being static means you can call it without the need of creating an instance. That is useful for example if you have a class Math and want to define a function that takes to arguments to calculate the sum of them. It wouldn't really be useful to create an instance of Math to do that, so you can declare as static and use it that way.
Here's a link to the docs with further info
http://www.php.net/manual/en/keyword.class.php
If functionY is static you can call ClassB::functionY(). Else you must create instance of Class B first. Like:
$instance = ClassB;
$instance->functionY();
But maybe you mean something else?
Looks like one of your class has a dependency to another one:
<?php
class A
{
public function x()
{
echo 'hello world';
}
}
class B
{
private $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function y()
{
$this->a->x();
}
}
$a = new A();
$b = new B($a);
$b->y();
Depending how your code looks like, if it makes sense, you can inject class A into y()
public function y(A $a)
{
// your code with $a
}
What's the proper way to get new self() to use the current instance's constructor? In other words, when I do:
class Foo{
function create(){
return new self();
}
}
Class Bar extends Foo{
}
$b = new Bar();
echo get_class($b->create());
I want to see: Bar instead of: Foo
class Foo{
function create(){
$c = get_class($this);
return new $c();
}
}
Class Bar extends Foo{
}
$b = new Bar();
echo get_class($b->create());
Store the class type in a temp variable and then return a new instance of it.
public static function create()
{
$class = get_called_class();
return new $class();
}
This should work.
class Foo{
public static function create()
{
$class = get_called_class();
return new $class();
}
}
class Bar extends Foo{
}
$a = Foo::create();
$b = Bar::create();
echo get_class($a), PHP_EOL, get_class($b);
Shows:
Foo Bar
UPD:
If you want non-statics, then:
<?php
class Foo{
public function create()
{
$class = get_class($this);
return new $class();
}
}
class Bar extends Foo{}
$a = new Bar();
$b = $a->create();
echo get_class($a), PHP_EOL, get_class($b);
?>
Shows:
Bar Bar
Class Bar extends Foo {
function create(){
return self;
}}
If you call a function from a child class then it if it will search the nearest parent to call it. This is inheritance.
The idea is to use polymorphism in this situation. Redeclaring a function from child override it's parent's activity. If you want to run it's parent function too then you have to call it like parent::callee();
This is what I have in foo.php
class Foo
{
public $foo = NULL;
public $foo2 = NULL;
public function setFoo ($foo, $foo2)
{
$this->foo = $foo;
$this->foo2 = $foo2'
}
}
This is what I have in foo3.php
class Foo3 extends Foo
{
public $foo3 = NULL;
public function setFoo3 ($foo3)
{
$this->foo = $foo3;
}
}
This is how I require it in my third file run.php:
require_once "foo.php";
require_once "foo3.php";
$foo = new Foo();
$foo->setFoo3("hello");
I get this error:
Fatal error: Call to undefined method Foo::setFoo3()
I'm not sure if the problem is how I'm requiring them. Thanks.
In your example, you are instantiating Foo, which is the parent and has no knowledge of the method setFoo3(). Try this:
class Foo3 extends Foo
{
...
}
require_once "foo.php";
require_once "foo3.php";
$foo = new Foo3();
$foo->setFoo3("hello");
At the first, in your foo.php shouldn't mark your fields public, because you set those values inside setFoo($foo1, $foo2) method. Instead, you may have something like:
<?php
class Foo
{
private $foo1;
private $foo2;
public function setFoo($foo1, $foo2) {
$this->foo1 = $foo1;
$this->foo2 = $foo2;
}
}
Then you should add extends keyword when declaring class Foo3, and another thing you need to include extending class file in the beginning of the file. In your case you may have something like the following in your foo3.php file:
<?php
require_once "foo.php";
class Foo3 extends Foo
{
public function setFoo3($foo3) {
$this->setFoo($foo3, "some foo3 specific value"); // calling superclass method
}
}
then you can create an instantiate of a Foo3 class in your run.php like so:
<?php
require_once "foo3.php";
$foo3 = new Foo3();
$foo3->setFoo3("bar");
and my advice, you should read a little about OOP techniques ;)
Of course that doesn't work. You've created a Foo object, and then tried to call foo3 on it. But Foo doesn't have a foo3 method.
How do i access the properties of class A from an object instantiated inside class A.
Like this;
class A()
public var1;
public obj1;
function __construct(){
$this->var1 = 'Hello World';
$this->obj1 = new B();
}
==============
class B()
function anything(){
#i want to access var1 from the calling class here ????
# how do i access var1 in the calling class
}
There's no direct way to do this. Dependency injection is a possibility:
class B {
protected $A = null;
public function __construct($A) {
$this->A = $A;
}
public function foo() {
$this->A->var1;
}
}
class A {
public function __construct() {
$this->obj1 = new B($this);
}
}
Here is my basic example:
class Foo {
public $toy = "car";
public function run() {
$this->toy = "train";
$bar = new Bar();
$bar->run();
}
}
class Bar extends Foo {
public function run() {
echo $this->toy;
}
}
$foo = new Foo();
$foo->run();
For some reason it will always echo car not train. What is the reason for this?
Foo run creates an object Bar, which has toy, initialized with car. So, the result is car, as expected.
Because you are creating a new Bar instance inside Foo::run. Changes you make to the Foo instance have no effect on the Bar instance. They are two different instances.
You can also see it this way: Whenever you instantiate an object from the child class, a new instance of the parent class will be created.
You can make the property static though, then it will be shared between all instances:
class Foo {
public static $toy = "car";
public function run() {
self::$toy = "train";
$bar = new Bar();
$bar->run();
}
}
class Bar extends Foo {
public function run() {
echo self::$toy;
}
}
What you do is "delegation": You create a new instance and call this instead of the parents method. What you are looking for is
parent::run();
This will call the parents method.