Variable declared in Public Function showing undefined [closed] - php

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've a super simple script here but I keep getting variable undefined as a warning any tips on how I can have this script run without error and do the same thing.
class person{
public $integer=4;
public $name;
public function __construct($name){
$this->name = $name;
}
public function pre(){
return "pre message";
}
public function after($name){
return "post message".$name;
}
};
$person1 = new person($name="jason");
$person2 = new person($name="bourne");
echo $person1->pre();
echo $person2->after();

You're setting the objects as $person1 and $person2, but calling them as $dog1 and $dog2

I think you want to construct your new objects just passing the desired parameter, not an assigment:
$person1 = new person("jason");
$person2 = new person("bourne");

You're not passing any arguments to after in $dog2->after();. Thus, $name is undefined in that method. If you want to use the class property $name, use $this->name there.
Also, as #MichaelKing pointed out, you are referring to $dog1 and $dog2, but setting the variables as $person1 and $person2.

You're setting the objects as $person1 and $person2 so it means
that You have to use it like
echo $person1->pre();
echo $person2->after();

You create object for the class person as $person1 and $person2. But you call the functions using $dog1 and $dog2, which is not declared or defined.

Related

Error while Accessing Private Func. from Public Func. inside a Class [closed]

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 the following class structure. When run the post() function, I receive an error Uncaught Error: Call to undefined function curl_post().
class POST {
private function encode($data) {
}
private function curl_post($data) {
encode($data);
}
public function post($data) {
$post = curl_post($data);
print_r($post);
}
}
What is causing this error? Do I have to use something like $this-> to access the private function?
To call sibling object methods, prefix the call with $this->:
class POST {
private function encode($data) {
}
private function curl_post($data) {
$this->encode($data);
}
public function post($data) {
$post = $this->curl_post($data);
print_r($post);
}
}
From the documentation:
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs...

Why won't my class variables get initialized by my _construct function? [closed]

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 learning to use the _construct function for initializing classes... and I seemed to be on the right path, but then at debug the class variables return empty.
Here's the code:
Class Access.php
class Access {
var $username='';
var $password='';
var $result=false;
var $error='';
/** OBJECT CONSTRUCTOR FUNCTION / CREATES ACCESS OBJECT**/
public function _construct($username, $password) {
$this->username = $username;
$this->password = $password;
}
}
and my test code:
include "Access.php";
$un = 'FakeUN';
$pw = 'FakePW';
$a = new Access($un,$pw);
echo $a->username;
echo $a->password;
this prints nothing on the screen; the class variables were not initialized.
If you could let me know what I'm doing wrong, I'd appreciate it!
Thank you
The name of the constructor method is __construct. All magic methods begin with two underscores.
You have to write 2x _!
You just forgot to write 2 underscores
So write:
public function __construct($username, $password) {
//do something
}

PHP 5.3 constructor method [closed]

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 am a Java/C# programmer who is trying to learn/finish a project in PHP.
Can anyone explain to me why "Composition" doesn't work in PHP 5.3 as one would expect from an object oriented language?
I have tried to research the issue, but due to term-confusion (making Google useless...) and bad documentation, I haven't been able to find anything useful yet.
<?php /*PHP VERSION 5.3.3*/
class MyClassOne
{
public function myFunctionOne()
{
echo "<p> My Function One </p>";
}
}
class MyClassTwo
{
private $myClassOne;
function __constructor() // WRONG WRONG WRONG - __construct() - and it works.
{
$this->myClassOne = new MyClassOne();
}
public function myFunctionTwo()
{
echo "<p> My Function Two </p>";
$this->myClassOne->myFunctionOne(); // This crashes the "application"
}
}
$myclassone = new MyClassOne();
$myclassone->myFunctionOne();
$myclasstwo = new MyClassTwo();
$myclasstwo->myFunctionTwo();
/*
Expectet result:
My Function One
My Function Two
My Function One
Real result:
My Function One
My Function Two
(application/runtime crash)
*/
?>
It will be highly appreciated if anyone can provide an explanation or show me the relevant documentation for this behavior.
You have a typo. Change __constructor to __construct and it will work correctly.
PHP constructors should be named __construct() as per the documentation.

PHP - New to OOP, already stuck [closed]

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 am getting started with OOP because I already wrote procedural code enough that I want to step up.
I started by creating a file 'user.class.php', wrote some code, and loaded in into the server.
<?php
class user {
private $name;
private $age;
function __construct($name, $age){
$this->name=$name;
$this->age=$age;
}
function getName() {
return $this->name;
}
function getAge() {
return $this->age;
}
}
$usr = new user('Alex', 16);
print($usr->getName.'<br>');
print($usr->getAge.'<br>');
I ran the code on the web server and got the following error:
Notice: Undefined property: user::$getName in /Library/WebServer/Documents/user.class.php on line 24
Same happened with the getAge function call.
If I can get this code working I will feel better by starting to write more OO code instead of just procedural code. Thanks in advance.
You're just missing your parenthesis for your method calls. Without them you are trying to get a property called getName which doesn't exist:
print($usr->getName().'<br>');
print($usr->getAge().'<br>');
$usr->getName means member variable
where you have a function so
$usr->getName()
is the correct way to call the function for that object
You are missing parenthesis in
print($usr->getName().'<br>');

Storing reference to other class object [closed]

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 come from a java background and m trying a hand at php. Right now I m trying to pass a object to constructor of a class and trying to store a reference to it inside the class and upon a function call to this call execute a method from the stored reference.
$phpBook = new Book("Php Book", 500);
$vihaan = new Person("Vihaan", $phpBook);
Person.php
class Person
{
private $_book;
private $_name;
public function __construct($name, $book)
{
$_this->_book = $book;
$_this->_name = $name;
}
on this line
$_this->_book = $book;
I get a warning.
PHP Warning: Creating default object from empty value in /home/vihaan/workspace/AdapterPattern1/Person.php on line 12
and this function call never enter the if block as $_book seems to be empty.
public function openBook($pageNumber = 0)
{
if(!empty($_book))
{
$_book->open($pageNumber);
}
}
It's called $this, not $_this!
$this->_book = $book;
$this->_name = $name;

Categories