The point is to execute a function in the constructor, like this for example.
$clas = new andrestest();
class andrestest{
function __construct(){
FunctionName();
}
public function FunctionName()
{
echo 10;
}
}
You can do two things
function __construct(){
$this->FunctionName();
}
or
function __construct(){
self::FunctionName();
}
Related
I am trying to access the contents of a variable from another class. I have the code below, I am expecting to get 'test' returned, I get nothing.
I assume this is because it is getting $abc_rank as empty. It is required that the variable is populated in the function itself.
Therefore how can I get $abc_rank to hold that echo and output via the other class?
<?php
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
$this->abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
$go = new class2();
?>
I know I can do:
public static $abc_rank = 'test';
But the population of the variable must be in a function.
I have read some of the other related answers and can't seem to get this to work.
In class1 :
Replace $this->abc_rank = 'test'; with $this::$abc_rank='test';
($abc_rank is a static property)
In class2 :
In your display function : replace
$test = class1::$abc_rank;
echo $test;
with
$test = new class1();
echo $test::$abc_rank;
(class1 isn't static)
Full code here :
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
//$this->abc_rank = 'test';
$this::$abc_rank='test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
//$test = class1::$abc_rank;
//echo $test;
$test = new class1();
echo $test::$abc_rank;
}
}
$go = new class2();
you have to create the class1 to run the constructor of this class.
class class1 {
public static $abc_rank;
public function __construct() {
$this->add_text();
}
public function add_text() {
self::$abc_rank = 'test';
}
}
class class2 {
public function __construct() {
$this->display();
}
public function display() {
$test = class1::$abc_rank;
echo $test;
}
}
new class1();
$go = new class2();
I have a class and two functions inside it as follows:
class MyClassName
{
protected function myFunction1()
{
// some code here
return $something;
}
public function myFunction2()
{
// some code here
return $somethingElse;
}
}
What I need to do is define a variable in myFunction1() and then use it in myFunction2(). What is the best practice to do that?
class MyClassName
{
public $var = 0;
protected function myFunction1()
{
// some code here
$this->var = ...;
return $something;
}
public function myFunction2()
{
// some code here
echo $this->var;
return $somethingElse;
}
}
Actually vars should be defined out of the function and then set a value. Then can be modified over all the script, by doing this->var
Make it a class property
class MyClassName
{
private $property;
public function __construct() {
$this->myFunction1();
}
protected function myFunction1()
{
// some code here
$this->property = 'an apple';
}
public function myFunction2()
{
// some code here
return $this->property;
}
}
Now test it:
$my_class = new MyClassName();
$something = $my_class->myFunction2();
echo $something;
I have php class(simple example):
<?php class test{
public function __construct() {
//some code
}
public function __destruct() {
//some code
}
public function echo1 {
//some code
return 1;
}
public function echo2 {
//some code
return 2;
}
}
How could I return results of this two functions echo1 and echo2 in class in one row don't creating two new objects for each function?
$obj= new Test;
echo $obj->echo1().$obj->echo2();
Also, capitalize class names, and you'll need parentheses on those functions:
class Test{
public function __construct() {
//some code
}
public function __destruct() {
//some code
}
public function echo1() {
//some code
return "Hello";
}
public function echo2() {
//some code
return "World";
}
}
$obj= new Test;
echo $obj->echo1()." ".$obj->echo2();
You may have to change your functions a little bit.
...
public function echo1() {
//some code
echo 1;
return $this;
}
public function echo2() {
//some code
echo 2;
return $this;
}
...
Then call it like this.
(new test())->echo1()->echo2();
I'd like to understand better how calls to functions work in OOP. I have the following sample:
class SomeClass {
function __construct(){
//run function do()
//run function include()
//run function run()
}
public function do($foo){
//do some stuff
}
public function include(){
require_once( CONSTANT . 'required.php' );
}
public function run(){
required_func();
}
}
$load_class = new SomeClass();
in required.php:
function required_func(){
$customerInfo = "info";
$customer = $this -> do($customerInfo); //--> This isn't right
return $customer;
}
What I'm trying to do is to have required_func() run the do() with the $customerInfo. So essentially: How to call a Class public function from another function included in the require_once file? Am I even remotely on track here?
Thanks for your help
$this isn't in scope for function required_func()
class SomeClass {
function __construct(){
//run function do()
//run function include()
//run function run()
}
public function do($foo){
//do some stuff
}
public function include(){
require_once( CONSTANT . 'required.php' );
}
public function run(){
required_func($this);
}
}
$load_class = new SomeClass();
and
function required_func($customerObject){
$customerInfo = "info";
$customer = $customerObject->do($customerInfo);
return $customer;
}
Calling __construct() function from __destruct(),
<?php
public function __construct() {
echo "Hi";
}
public function __destruct() {
$this->__construct();
}
?>
will it create infinite loop?
No, but this will:
class Test {
public function __construct() {
echo "Hi";
}
public function __destruct() {
new Test();
}
}
new Test();
Example: http://ideone.com/94XUg
No, it won't. __construct is just regular function while called directly instead of using new ClassName;