For some reason I cant run the change() function and everything after it stops. I try with $this->change() but the effect is the same. If I run the function in a php file its working and the num is changing.
class Test extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$num = 1;
change($num);
function change($num,$rand=false){
echo 'inside function'; // this is not shown
if($num < 4) {
$rand = rand(1,9);
$num = $num.$rand;
change($num,$rand);
} else {
echo $num;
}
}
echo 'done'; // this is not shown
}
}
You will probably be better off calling a private or public function to process data ( or for more complex/involved processes a library ).
ie
class Test extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function home()
{
$num = 1;
echo $this->_change($num);
echo 'done'; // this is not shown
}
// private or public is w/o the underscore
// its better to use private so that CI doesn't route to this function
private function _change($num, $rand = FALSE)
{
if($num < 4) {
$rand = rand(1,9);
$num = $num + $rand;
$this->_change($num,$rand);
} else {
return $num;
}
}
}
lol, you are trying to write function inside function.
try running your class with this code
class Test extends CI_Controller {
function __construct(){
parent::__construct();
}
public function home(){
$num = 1;
$this->change($num);
echo 'done'; // this is not shown
}
public function change($num,$rand=false){
echo 'inside function'; // this is not shown
if($num < 4) {
$rand = rand(1,9);
$num = $num.$rand;
change($num,$rand);
} else {
echo $num;
}
}
}
Related
I have this class :
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct()
{
$this->getPad($this->i);
}
public function getPad($i)
{
return ''.str_pad($i,4,'0',0);
}
}
And I use it in this way :
$cod = new codici();
$cod_cliente = $cod->i = 1; //return 1
$cod_cliente = $cod->getPad(1); //return 0001
If I call the class direct, __constructor call internal method getPad and returns wrong answer '1'. Instead, if I call the method getPad return the correct value '0001'.
Why can't I use $cod_cliente=$cod->i=1 ?
$cod_cliente = $cod->i = 1;
It will set value for $cod_cliente and $cod->i both to 1. So when you print $cod_cliente, it will show 1.
But in case $cod_cliente = $cod->getPad(1), code to add padding executes and return 0001.
If you want your constructor to return something you should give it a parameter. And since your getPad($i) returns something you'd need to echo/print the results.
<?php
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct($parameter)
{
$this->i = $parameter;
echo $this->getPad($this->i);
}
public function getPad($i)
{
return ''.str_pad($i,4,'0',0);
}
}
This will allow you to call your class like this:
$c = new codici(3);
which would echo 0003.
this is right code:
class codici {
public $i;
public $len;
public $str;
public $type;
function __construct($parameter)
{
$this->i = $this->getPad($parameter);
}
public function getPad($i)
{
return str_pad($i,4,'0',0);
}
}
now work:
$c= new codici(1);
echo $c->i;//return 0001
echo $c->getPad(1);//return 0001
thank a lot.
I have the following code:
<?php
class Node{
public $left,$right;
public $data;
function __construct($data)
{
$this->left=$this->right=null;
$this->data = $data;
}
}
class Solution{
public function insert($root,$data){
if($root==null){
return new Node($data);
}
else{
if($data<=$root->data){
$cur=$this->insert($root->left,$data);
$root->left=$cur;
}
else{
$cur=$this->insert($root->right,$data);
$root->right=$cur;
}
return $root;
}
}
public function getHeight($root) {
$heightLeft = 0;
$heightRight = 0;
if ($root->left != null) {
$heightLeft = getHeight($root->left) + 1;
}
if ($root->right != null) {
$heightRight = getHeight($root->right) + 1;
}
echo "heightRigh is $heightRight\n";
echo "heightLeft is $heightLeft\n";
$ans = ($heightLeft > $heightRight ? $heightLeft : $heightRight);
return $ans;
}
}//End of Solution
$myTree=new Solution();
$root=null;
$T=intval(fgets(STDIN));
while($T-->0){
$data=intval(fgets(STDIN));
$root=$myTree->insert($root,$data);
}
$height=$myTree->getHeight($root);
echo $height;
?>
When I run it with the inputs
1
1
it gives the correct results.
But when I run it with the inputs
2
1
2
I get the error:
PHP Fatal error: Call to undefined function getHeight() in C:\git\phpStudy\CallingAFunction.php on line 36
Fatal error: Call to undefined function getHeight() in C:\git\phpStudy\CallingAFunction.php on line 36
I am new to php and can't figure out what I am doing wrong. Thank you.
The answer is very easy. In short your problem is this:
a) leads to fatal error as described:
class Solution{
public function getHeight($a) {
if($a==true) {
return getHeight(false);
}
return "hello";
}
}
$a = new Solution();
echo $a->getHeight(true);
b) works:
class Solution{
public function getHeight($a) {
if($a==true) {
return $this->getHeight(false);
}
return "hello";
}
}
$a = new Solution();
echo $a->getHeight(true);
You need to reference to the class if you want to call a function inside the class. Use $this->.
In line 36 you have a recursive function call to get height. The function is not found. Correct solution is therefore:
<?php
class Node{
public $left,$right;
public $data;
function __construct($data)
{
$this->left=$this->right=null;
$this->data = $data;
}
}
class Solution{
public function insert($root,$data){
if($root==null){
return new Node($data);
}
else{
if($data<=$root->data){
$cur=$this->insert($root->left,$data);
$root->left=$cur;
}
else{
$cur=$this->insert($root->right,$data);
$root->right=$cur;
}
return $root;
}
}
public function getHeight($root) {
$heightLeft = 0;
$heightRight = 0;
if ($root->left != null) {
$heightLeft = $this->getHeight($root->left) + 1;
}
if ($root->right != null) {
$heightRight = $this->getHeight($root->right) + 1;
}
echo "heightRigh is $heightRight\n";
echo "heightLeft is $heightLeft\n";
$ans = ($heightLeft > $heightRight ? $heightLeft : $heightRight);
return $ans;
}
}//End of Solution
$myTree=new Solution();
$root=null;
$T=intval(fgets(STDIN));
while($T-->0){
$data=intval(fgets(STDIN));
$root=$myTree->insert($root,$data);
}
$height=$myTree->getHeight($root);
echo $height;
?>
I have this php code and i want to call the function firstnameLength() from the class formular_validiation.
class formular_validiation
{
private static $minLength = 2;
private static $maxLength = 250;
public static function firstname() {
function firstnameLength($firstnameLength){
if ($firstnameLength < self::$minLength){
}
elseif ($firstnameLength > self::$maxLength) {
}
}
function firstnameNoSpace($firstnameNoSpace) {
preg_replace(" ", "", $firstnameNoSpace);
}
}
}
I thougth about something like:
formular_validiation::firstname()::firstnamelength()
but this is wrong.
What you are looking for is called method chaining but if you want to call the first method statically you should do something like:
class FormularValidation
{
private $minLength = 2;
private $maxLength = 250;
private $firstname;
public function __construct($firstname)
{
$this->firstname = $firstname;
}
public static function firstname($firstname) {
return new self($firstname);
}
public function firstnameLength()
{
$firstnameLength = strlen($this->firstname);
if ($firstnameLength < $this->minLength){
return 'something';
}
elseif ($firstnameLength > $this->maxLength) {
return 'something else';
}
}
public function firstnameNoSpace()
{
return preg_replace(" ", "", $this->firstname);
}
}
Usage:
$firstnameLength = FormularValidation::firstname('Mihai')->firstnameLength();
I am testing with Codeigniter3.0.6 on User agent as below function then I try to testing IPhone, and Chrome inspect device mode but I got only number 1.
I want to check if I view this website in Mobile phone it will echo number 2
if in PC browser echo number1 if can't detect browser show 0.
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Main_Controller extends MY_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
var_dump($this->CheckDevices());
exit();
$this->data['deviceType'] = $this->CheckDevices();
if ($this->data['deviceType'] == 1) {
$this->set_navigation();
}
elseif ($this->data['deviceType'] == 2) {
return false;
}elseif($this->data['deviceType'] == 0){
return false;
}
}
private function CheckDevices()
{
$this->load->library('user_agent');
$agent = '';
if ($this->agent->is_browser()) {
$agent = 1;
} elseif ($this->agent->is_mobile()) {
$agent = 2;
} else {
$agent = 0;
}
return $agent;
}
private function set_navigation()
{
$this->load->library('session');
$this->load->library("nav_libs");
return $this->data['menus'] = $this->nav_libs->navigation();
}
}
?>
you can try this
application/core/My_Core.php
class My_Core extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function detect_dvice(){
$this->load->library('user_agent');
if( $this->agent->is_mobile()){
$_is_mobile = 1;
}
else{
$_is_mobile = 2;
}
return $_is_mobile
}
}
normal controller
application/controllers/Test_controller.php
class Test_controller extends My_Core{
public function __construct(){
parent::__construct();
}
public function index(){
echo $this->detect_dvice();
}
}
<?php
class Timer {
static private $s;
function __construct()
{
self::$s = self::getmicrotime();
}
static public function Start()
{
self::$s = self::getmicrotime();
}
static public function Fetch($decimalPlaces = 6)
{
return number_format((self::getmicrotime() - self::$s), $decimalPlaces);
}
static public function getmicrotime()
{
return array_sum(explode(' ', microtime()));
}
}
class T extends Thread {
public function run() {
test();
}
}
function test()
{
$n = 0;
for($i=1;$i<9999999;$i++)
{
$n+=$i;
}
echo '#';
}
//+++++++++++++++++++++++START DEMO ++++++++++++++++++++++++++
Timer::Start();
//DEMO1,TIME:3.679208 second(s).
$ts = array();
while (count($ts)<10) {
$t = new T();
$t->start();
$ts[]=$t;
}
$ts = array();
//DEMO2,TIME:6.876037 second(s).
/* for($k=0;$k<10;$k++)
{
$t = new T();
$t->start();
} */
echo '<br />Processed in '.Timer::Fetch().' second(s).';
?>
I am reading http://pthreads.org/ on the topic of pthreads and i have this questions,I want to test php multithreading ,but when i use for to create multithreading,but it is not the result I want.
In fact the DEMO1 is correct.
But why the second DEMO elapsed_time 6.876037 second ?
DEMO2 why canot use for to create multithreading ?