Skip to content
Advertisement

Tag: constructor

Can you return another instance of the same class from the constructor function (Singleton Pattern)?

I just came across an answer to another question, https://stackoverflow.com/a/32954854/1682790 In that answer, they use the following: But everything else I’ve seen seems to contradict this section of code, namely that in PHP a constructor always returns the object that’s been instantiated, and ignores the return statement altogether. If that’s actually the case, then this isn’t actually creating a singleton,

PHP Class for adding two numbers

I am doing a challenge in PHP with OOP/classes and I am not sure why I am not passing the test case for two random numbers being added together. My syntax must be off somewhere, but I am very new to PHP so I am having trouble figuring it out. Here are the instructions for my challenge: Write a class

how to use function from another class in flight php

I am new in flight php. I need some help, I create two classes client.class.php and deliveryServiceConnector.class.php and i have index.php. I want to use function from deliveryServiceConnector.class.php in client.class.php so I write this code: I got this error: Undefined variable: connector (8) Any idea how can i fix my error, Thanks Answer You’re not using the same thing for

What am I doing wrong with my optional PHP arguments?

I have a the following class: So from my interpretation I should be able to pass either $id or $humanIdentifier to that constructor, neither or both if I wanted. However, when I call the code below I am finding that its the $id in the constructor args being set to hello world and not the $humanIdentifier, despite me specifying the

Codeigniter variables from constructor are undefined

I’m using CI’s Auth Tank library to query records for certain users. The variable $user_id = tank_auth->get_user_id(); grabs the user id from the session. I want to pull records where user_id = $user_id. From what I understood, constructors can load variables each time a class is initiated. Sort of like global variables. So I figured I’ll set my $user_id in

Advertisement