how to delete object in a constructor in PHP? - php

I have my code here and I dont know how to delete object with this kind of method
$patient->deletePatient(2) im a beginner and I cant find an answer in internet and I think the way i use var_dump was incorrect please help im stuck
this is my code:
<?php
$index = 0;
class Clinic {
public $name;
public $age;
public $gender;
function Patient($name,$age,$gender){
$this->name = $name;
$this->age = $age;
$this->gender = $gender;
$id = $name;
$id = $age;
$id = $gender;
}
function assignPatient($name,$age,$gender){
$this->Patient($name,$age,$gender);
}
function deletePatient($id=0){
$this->Patient($id);
var_dump((unset) $id);
}
}
$patient = new Clinic;
$patient->assignPatient("Patrick star",18,"Male");
$patients[$index] = $patient;
$index++;
$patient->assignPatient("SpongeBob Squarepants",17,"Male");
$patients[$index] = $patient;
$index++;
$patient->assignPatient("Eugene Krab",28,"Male");
$patients[$index] = $patient;
$index++;
$patient->deletePatient(2);
foreach($patients as $patient)
{
echo $patient->name . " ";
echo $patient->age . " ";
echo $patient->gender . "\n";
}

Patient and Clinic should be separate objects. Then you can store Patient objects inside Clinic->patients array.
class Patient{
private $name;
private $age;
private $gender;
public function __construct($name, $age, $gender){
$this->name = $name;
$this->age = $age;
$this->gender = $gender;
}
public function getName(){
return $this->name;
}
public function getAge(){
return $this->age;
}
public function getGender(){
return $this->gender;
}
}
class Clinic{
private $patients = [];
public function getPatients(){
return $this->patients;
}
public function assignPatient($name, $age, $gender){
$this->patients[] = new Patient($name, $age, $gender);
}
public function deletePatient($index){
unset($this->patients[$index]);
}
}
$clinic = new Clinic();
$clinic->assignPatient("Patrick star",18,"Male");
$clinic->assignPatient("SpongeBob Squarepants",17,"Male");
$clinic->assignPatient("Eugene Krab",28,"Male");
$clinic->deletePatient(1);
var_dump($clinic->getPatients());

<?php
class Patient {
public $name;
public $age;
public $gender;
function __construct($name, $age, $gender) {
$this->name = $name;
$this->age = $age;
$this->gender = $gender;
}
}
class Clinic {
private $patients = [];
private $patientIndex = 0;
function assignPatient($patient) {
$this->patients[$this->patientIndex] = $patient;
++$this->patientIndex;
return $this->patientIndex;
}
function deletePatient($id) {
if(array_key_exists($id, $this->patients)) {
unset($this->patients[$id]);
return true;
}
return false;
}
function getPatients() {
return $this->patients;
}
}
$clinic = new Clinic();
$patient1 = new Patient("Patrick star",18,"Male");
$id1 = $clinic->assignPatient($patient1);
$patient2 = new Patient("SpongeBob Squarepants",17,"Male");
$id2 = $clinic->assignPatient($patient2);
$patient3 = new Patient("Eugene Krab",28,"Male");
$id3 = $clinic->assignPatient($patient3);
$clinic->deletePatient($id2);
foreach($clinic->getPatients() as $patient) {
var_dump($patient);
}

Related

How to update value in a class objecct in php

I have one file user.php with constructor for the client:
class Client{
public $name;
public $pin;
public $balance;
function __construct($name, $pin, $balance){
$this->name = $name;
$this->pin = $pin;
$this->balance = $balance;
}
}
$client1 = new Client("Alan", "0201", 2000);
?>
Also I have a file where i do some math to the client object:
$balance = $client1->balance;
function withdrow($amount, $balance){
$currentBalance = $balance - $amount;
if($balance < $amount){
echo "You are low on balance";
}
else{
$client1->balance = $currentBalance;
return $currentBalance;
}
}
And 3rd html file where I include First file with the client and I want to echo the updated value of balance on the screen.But its print the value set in client.How to update the balance.
My code look like this right now but setter not change the value balance:
class Client{
public $name;
private $pin;
private $balance;
function __construct($name, $pin, $balance){
$this->name = $name;
$this->pin = $pin;
$this->balance = $balance;
}
private function setPin($pin){
$this->pin = $pin;
}
public function getPin(){
return $this->pin;
}
public function setBalance($balance){
$this->balance = $balance;
}
public function getBalance(){
return $this->balance;
}
}
$client1 = new Client("Alan", "0201", 2000);
you must call function with call by reference
function withdrow($amount, &$balance){ /** call by reference */
$currentBalance = $balance - $amount;
if($balance < $amount){
echo "You are low on balance";
}
else{
$client1->balance = $currentBalance;
return $currentBalance;
}
}

Property of a PHP Object itself is another Object of another class

I am pretty sure that we can store reference to an Object's property with an Object of a different class.
But I am stuck with this code.
The __construct() functions of both metro and town assigns the values for $name and $pop of those objects . But I need the __construct() function of the class city to create a new object of either class metro or class town depending on the value of $pop of the object of class city
<?php
class metro
{
public $name;
public $pop;
function __construct($name,$pop)
{
$this->name = $name;
$this->pop = $pop;
}
}
class town
{
public $name;
public $pop;
function __construct($name,$pop)
{
$this->name = $name;
$this->pop = $pop;
}
}
class city
{
public $name;
public $pop;
public $derived_city;
function __construct($name,$pop)
{
$this->name = $name;
$this->pop = $pop;
if ($this->pop >= 50)
{
$derived_city = new metro($this->name,$this->pop);
}
else
{
$derived_city = new town($this->name,$this->pop);
}
}
}
$city1 = new city("Bombay",100);
echo $city1->derived_city->pop;
?>
Do this:
class metro
{
public $name;
public $pop;
function __construct($name,$pop)
{
$this->name = $name;
$this->pop = $pop;
}
}
class town
{
public $name;
public $pop;
function __construct($name,$pop)
{
$this->name = $name;
$this->pop = $pop;
}
}
class city
{
public $name;
public $pop;
public $derived_city;
function __construct($name,$pop)
{
$this->name = $name;
$this->pop = $pop;
if ($this->pop >= 50)
{
$derived_city = new metro($this->name,$this->pop);
}
else
{
$derived_city = new town($this->name,$this->pop);
}
$this->derived_city = $derived_city;
}
}
$city1 = new city("Bombay",100);
print_r($city1->derived_city);
echo $city1->derived_city->pop;

Is it possible to chain static together with non-static method in PHP?

Here is my sample code Class User but not working when I added the static method with the public methods:
<?php
namespace App\Classic;
class User
{
public $username;
public static $upassword;
public $age;
public $message;
public function username($username)
{
$this->username = $username;
echo $this->username."<br>";
return $this;
}
public static function password($upassword)
{
self::$upassword = $upassword;
echo self::$upassword."<br>";
}
public function age($age)
{
$this->age = $age;
echo $this->age."<br>";
return $this;
}
public function message($message)
{
$this->message = $message;
echo $this->message."<br>";
return $this;
}
}
and this is the side effect of chaining method:
$user = new User();
$user::password('secret')
->username('admin')
->age(40)
->message('lorem ipsum');
I dont know what is the logic behind doing this, but still this solution will be helpful.
Try this code snippet here
<?php
namespace App\Classic;
ini_set('display_errors', 1);
class User
{
public $username;
public static $upassword;
public static $currentObject=null;//added this variable which hold current class object
public $age;
public $message;
public function __construct()//added a constructor which set's current class object in a static variable
{
self::$currentObject= $this;
}
public function username($username)
{
$this->username = $username;
echo $this->username . "<br>";
return $this;//added this statment which will return current class object
}
public static function password($upassword)
{
self::$upassword = $upassword;
echo self::$upassword . "<br>";
return self::$currentObject;
}
public function age($age)
{
$this->age = $age;
echo $this->age . "<br>";
return $this;
}
public function message($message)
{
$this->message = $message;
echo $this->message . "<br>";
return $this;
}
}
$user = new User();
$user::password('secret')
->username('admin')
->age(40)
->message('lorem ipsum');

php7 object collection + properties

How can I create an object in php (like c#)
for exemple user with 3 properties
class Users
{
private $name;
private $age;
private $sex;
public function __construct($name, $age, $sex) {
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
}
how can I add users in the collection
and then loop users collection to get
$user->name
$user->age
$user->sex
thank you
From you given example, it might be achieved like that:
class User
{
const
SEX_M = 1,
SEX_F = 2
;
private
$name,
$age,
$sex
;
public function __construct($name, $age, $sex) {
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
public function dump()
{
echo "name: $this->name, age: $this->age, sex: "
. ($this->sex === User::SEX_M ? 'm' : 'f') . "<br>\n";
}
}
$collection = array();
$collection[] = new User('Tom' , 10, User::SEX_M);
$collection[] = new User('Jerry', 11, User::SEX_F);
foreach ($collection as $user)
$user->dump();

Managing collection of items through adding/removing

I want have an in-memory data structure to be able to add or remove an item (in this instance a student) into some sort of table (just like a shopping cart) from the collection class I have created. At the moment, it just displays students. For instance, if I click add student, it will pop up below, and I can delete this student from below also.
How I could implement this?
Here is my Member.php class
<?php
class Member {
private $name;
private $age;
private $gender;
private $course;
public function __construct($name,$age, $gender, $course){
$this->name = $name;
$this->age = $age;
$this->gender = $gender;
$this->course = $course;
}
public function setName($name) { //Sets the age value
$this->name = $name;
}
public function setAge($age) { //Sets the age value
$this->age = $age;
}
public function setGender($gender) { //Sets the gender value
$this->gender = $gender;
}
public function setCourse ($course) {
$this->course = $course;
}
public function getName() { //Gets the name value
return $this->name;
}
public function getAge() { //Gets the age value
return $this->age;
}
public function getGender() { //Gets the gender value
return $this->gender;
}
public function getCourse() {
return $this->course;
}
}
?>
Here is my ObjectCollection.php
<?php
class ObjectCollection
{
//This is an array to hold line items
private $items_array ;
private $itemCounter; //Count the number of items
public function __construct() {
//Create an array object to hold line items
$this->items_array = array();
$this->itemCounter=0;
}
public function getItemCount(){
return $this->itemCounter;
}
public function addItem($item) {
$this->itemCounter++;
$this->items_array[] = $item;
}
public function getItem($index) {
return $this->items_array[$index];
}
}
?>
And finally displaying this through testcollection.php
<?php
$ObjColl = new ObjectCollection();
$member1 = new Member("Jessica Davidson", 21, "Female", "Computing");
$ObjColl->addItem($member1);
$member2 = new Member("Lucy Barnes", 22, "Female", "History");
$ObjColl->addItem($member2);
$member3 = new Member("Mark Smith", 24, "Male", "Social Science");
$ObjColl->addItem($member3);
for($i = 0;$i < $ObjColl->getItemCount();$i++){
$item = $ObjColl->getItem($i);
if ($item instanceof Member) {
print "<br> University Member: ";
}
print "Name: " . $item->getName();
print ". Age: " . $item->getAge();
print ". Gender: " . $item->getGender();
print ". Enrolled on: " .$item->getCourse() . " course<br>";
}
?>
At first if your ObjectCollection must collect only objects of Member class, use parameter type declaration. It’s good practice in OOP.
public function addItem(Member $item)
At second if you want work with ObjectCollection like with array, implement ArrayAccess and Iterator interfaces.
Example
<?php
class Member{
private $__name;
public function __construct($name){
$this->__name = $name;
}
public function getName(){
return $this->__name;
}
}
class MemberCollection implements ArrayAccess, Iterator{
private $__Collection = [];
private $__position = 0;
public function __construct(){
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->__Collection[] = $value;
} else {
$this->__Collection[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->__Collection[$offset]);
}
public function offsetUnset($offset) {
unset($this->__Collection[$offset]);
}
public function offsetGet($offset) {
return isset($this->__Collection[$offset]) ? $this->__Collection[$offset] : null;
}
function rewind() {
$this->__position = 0;
}
function current() {
return $this->__Collection[$this->__position];
}
function key() {
return $this->__position;
}
function next() {
++$this->__position;
}
function valid() {
return isset($this->__Collection[$this->__position]);
}
public function addItem(Member $Member){
$this->offsetSet(null, $Member);
}
}
$MC = new MemberCollection();
$Member1 = new Member('Name 1');
$Member2 = new Member('Name 2');
$MC->addItem($Member1);
$MC->addItem($Member2);
foreach ($MC as $Member){
echo '<br>' . $MC->key() . ':<br>';
var_dump($Member->getName());
}
unset($MC[0]); //Delete member from collection
?>

Categories