Storing reference to other class object [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 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;

Related

I get a "Non static method" error after upgrading to PHP 8.1 [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 7 months ago.
Improve this question
I have the following PHP error after upgrading from PHP7.4 to PHP8.1
$result = CarsDAO::selectCarsByColor($idCar, self::CARS_PER_PAGE, 0);
Non static method 'selectCarsByColor' should not be called statically
Any ideas how to rewrite this to be OK?
As the error says, the method is not static in the CarsDAO class, so you should call it on an instance.
$car = new CarsDAO();
$result = $car->selectCarsByColor($idCar, self::CARS_PER_PAGE, 0);
or repair the class by making the method static adding the static keyword in front of the method declaration. Read about static in the manual.
class CarsDAO {
public static function selectCarsByColor($idCar, $carsPerPage, $zeroThing) {
// code here
}
}

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...

abstract class cannot be declared as public? [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 writing a code in drupal custom module
It throws error while adding public keyword before abstract
public abstract class testParent {
public function abc() {
return 1;
}
}
// Class to extent abstract class property
class testChild extends testParent {
public function xyz() {
//body of your function
}
}
$a = new testChild();
print $a->abc();
The members of a class may be limited in visibility, but all classes are essentially 'public' in PHP.
http://php.net/manual/en/language.oop5.visibility.php
You will get a syntax error if you try to use the public keyword on a class (something like PHP Parse error: syntax error, unexpected 'public' (T_PUBLIC))

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>');

Variable declared in Public Function showing undefined [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'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.

Categories