Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
My php code is:
<?php
class Product {
var $product_name;
var $retailer;
function __constructor($product, $retailer) {
$this->product_name = $product;
$this->retailer = $retailer;
}
function getProduct() {
return $this->product_name;
}
}
$product_arr = array();
for ($f = 0; $f < 100; $f++) {
array_push($product_arr, new Product("asd", "xcxcxc"));
}
print_r($product_arr);
?>
The code is pretty simple, I have a class called "Product", I build an array consists of 100 Product object, but when I tried to print the array, I found out all the object's product_name and retailer fields are empty. Not sure why this happens.
Wrong name:
function __constructor($product, $retailer) {
^^^
PHP's standard constructor name is simply __construct (no or). So you never actually called a constructor, which means your variable assignments never executed.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have an error probably because of the syntax and I cant find how to do it correctly.
This is my code
function myFunction($id = 'ID')
{
if (!$this->$id) {
// TO DO SOMETHING
}
}
I have the next error
Notice: Undefined property: Base::$ID ..... on line 278
I tried with
if (!$this->{$id}) {
but nothing
anyone know the correct syntax is. I cant find it on google neither.
You may use property_exists instead of using direct access if(!$this->$id):
function myFunction($id = 'ID')
{
if (!property_exists($this, $id)) {
// TO DO SOMETHING
}
}
If you are defining a property on your class, you should access it like such:
$this->id
not
$this->$id
I created an example how your code might work, and I kept the idea of accessing a property by a string. The first step is to have the class actually have a field named "ID". I set this field value in the constructor.
class My {
private $ID;
public function __construct($id) {
$this->ID = $id;
}
public function myFunction($id='ID'){
if(!property_exists($this, $id)){
echo "$id not found";
} else {
echo $this->$id;
}
}
}
$my = new My(4);
$my->myFunction();
Simply use
if(!$this->id) {
or
if(!$id) {
Try this method to get the output.
$this->id
or
$id
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have declared an array assigned to a variable called $PHPFiles but when I try to do anything with the array inside the function such as printing it in the example below I am getting an error stating it is undefined, any help in trying to solve this would be appreciated as I have tried making the variable global and public with no luck.
<?php
$PHPFiles = array();
$PHPFiles[] = 'sql1.php';
$PHPFiles[] = 'sql2.php';
$PHPFiles[] = 'sql3.php';
function push($PHPFiles) {
print_r(array_values($PHPFiles));
}
push();
?>
Cheers,
Jamie
Your function push requires an argument $PHPFiles, use it like this:
push($PHPFiles);
try this
<?php
$PHPFiles = array();
$PHPFiles[] = 'sql1.php';
$PHPFiles[] = 'sql2.php';
$PHPFiles[] = 'sql3.php';
function push($PHPFiles) {
print_r(array_values($PHPFiles));
}
// here you were not passed the variable while calling the `push()` method
push($PHPFiles);
?>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to check if user is logged on site or not, so I made a function:
function isLogged()
{
if($_SESSION['logged']=='1')
{
return true;
}
elseif($_SESSION['logged']!=='1')
{
return false;
}
}
On my site Im trying to induce function like this:
if('isLogged')
{
echo 'yes';
}
else
{
echo 'no';
}
Even if $_SESSION['logged'] is set to 0 (I just checked by echo value), it returns "yes". What is wrong with this code?
You have to actual call the function, not check if the string returns true:
if(isLogged())
{
echo 'yes';
}
else
{
echo 'no';
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm fairly new to PHP so I was wondering whether or not you could have to 'commands' in an if statement, like:
$value = 1;
$values = 2;
if ($value == '1' & $values == '2') {
do something
} else {
do something else
}
Would that work? I probably have the syntax wrong however you get the gist. Haha
Thanks for the help.
if you are new to use conditional operators read first before implement.
http://php.net/manual/en/language.operators.comparison.php
$value = 1;
$values = 2;
if ($value == '1' && $values == '2') {
// do something
} else {
// do something else
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I was making a class in PHP for validation of a contact form, when I hit a problem. I eventually tracked it down to an array within the class, and wrote a test script to try to solve the problem:
<?php
class Object {
public $testArray = array(5);
function __construct() {
$testArray[] = 7;
}
function addNumber() {
$testArray[] = mt_rand(10,20);
}
function returnArray() {
return var_dump($testArray);
}
}
$object = new Object;
$object->addNumber();
echo($object->returnArray());
var_dump($object->testArray);
This outputs NULL array(1) { [0]=> int(5) }.
I am confused as to why this does not work. Eventually I would like the array to be private, but I can't find a way similar to get and set in C#. Any ideas?
Give this a go:
class Object {
public $testArray = array(5);
function __construct() {
$this->testArray[] = 7;
}
function addNumber() {
$this->testArray[] = mt_rand(10,20);
}
function returnArray() {
return var_dump($this->testArray);
}
}
You have to reference the class variable using $this - otherwise it'll try to set a variable local to the function you're in.
You can use PHP's magic __set() and __get() methods for similar functions to C# that you mentioned: PHP Overloading.
$testArray[] = mt_rand(10,20);
Should be:
$this->testArray[] = mt_rand(10,20);
$testArray is just another local variable (local to the function, no the class). If it has no value it is null. This is consistent with the output you're seeing. $object->testArray is never modified.
You have to access class variables using $this. So $testArray should be $this- >testArray.