Tagged Questions
79
votes
8answers
52k views
How to initialize static variables
I have this code:
private static $dates = array(
'start' => mktime( 0, 0, 0, 7, 30, 2009), // Start date
'end' => mktime( 0, 0, 0, 8, 2, 2009), // End date
'close' => ...
76
votes
9answers
14k views
When to use static vs instantiated classes
PHP is my first programming language. I can't quite wrap my head around when to use static classes vs instantiated objects.
I realize that you can duplicate and clone objects. However in all of my ...
54
votes
10answers
44k views
How do I get a PHP class constructor to call its parent's parent's constructor
I need to have a class constructor in PHP call its parent's parent's (grandparent?) constructor without calling the parent constructor.
// main class that everything inherits
class Grandpa
{
...
47
votes
12answers
36k views
Can you create instance properties dynamically in PHP?
Is there any way to create all instance properties dynamically? For example, I would like to be able to generate all attributes in the constructor and still be able to access them after the class is ...
45
votes
7answers
37k views
php static function
I have a question regarding static function in php.
let's assume that I have a class
class test {
public function sayHi() {
echo 'hi';
}
}
if I do test::sayHi(); it works without a ...
44
votes
3answers
27k views
instantiate a class from a variable in PHP?
I know this question sounds rather vague so I will make it more clear with an example:
$var = 'bar';
$bar = new {$var}Class('var for __construct()'); //$bar = new barClass('var for __construct()');
...
42
votes
12answers
57k views
Can I extend a class using more than 1 class in PHP?
If I have several classes with functions that I need but want to store separately for organisation, can I extend a class to have both?
i.e. class a extends b extends c
edit: I know how to extend ...
38
votes
2answers
34k views
Getting DOM elements by Class name
I'm using PHP DOM and I'm trying to get an element within a DOM node that have a given class name. What's the best way to get that sub-element?
34
votes
4answers
22k views
What does new self(); mean in PHP?
I've never seen code like this:
public static function getInstance()
{
if ( ! isset(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
Is it the ...
34
votes
5answers
79k views
PHP Fatal error: Using $this when not in object context
I've got a problem:
I'm writing a new WebApp without a Framework.
In my index.php im using: require_once('load.php');
and in * load.php* I'm using require_once('class.php'); to load my class.php.
...
33
votes
5answers
83k views
Calling a function within a Class method?
I have been trying to figure out how to go about doing this but I am not quite sure how.
Here is an example of what I am trying to do:
class test {
public newTest(){
function ...
32
votes
2answers
551 views
How does a class extension or Interface work?
Have come across this so many times and am not sure why so it got me curious. Some classes work before they are declared and others don't;
Example 1
$test = new TestClass(); // top of class
class ...
29
votes
6answers
19k views
Purpose of PHP constructors
I am working with classes and object class structure, but not at a complex level – just classes and functions, then, in one place, instantiation.
As to __construct and __destruct, please tell me very ...
29
votes
5answers
21k views
When to use a Class vs. Function in PHP
The lightbulb has yet to go on for this...
I'd really love an easy to understand explanation of the advantage to using a class in php over just using functions.
Here's a simple example of the ...
27
votes
2answers
7k views
Finding the PHP File (at run time) where a Class was Defined
Is there any reflection/introspection/magic in PHP that will let you find the PHP file where a particular class (or function) was defined?
In other words, I have the name of a PHP class, or an ...
26
votes
1answer
3k views
How to access parent object from lambda functions?
I have a recursive lambda function in one of my objects, and it needs to access the object's mysqli connection. This attempt
$recfunc = function($id, $name) use($this) {
Produced an unreasonable ...
25
votes
7answers
23k views
Dynamic static method call in PHP?
Please could someone experienced in PHP help out with the following. Somewhere in my code, I have a call to a public static method inside a non-instantiated class:
$result = ...
25
votes
8answers
8k views
Determining what classes are defined in a PHP class file
Given that each PHP file in our project contains a single class definition, how can I determine what class or classes are defined within the file?
I know I could just regex the file for class ...
22
votes
3answers
8k views
How to properly set up a PDO connection
From time to time I see questions regarding connecting to database.
Most answers is not the way I do it, or I might just not get the answers correctly. Anyway; I've never thought about it because the ...
22
votes
4answers
18k views
How do I Use Inner Classes in PHP?
I'm from a Java background, and I want to use an inner class in php. Every time I put the inner class though, I get a syntax error. Is this possible with PHP? Also, how do I reference the outer class? ...
22
votes
1answer
2k views
PHP class instantiation. To use or not to use the parentheses?
I've always assumed that - in the absence of constructor parameters - the parentheses (curly brackets) follow the class name when creating a class instance, were optional, and that you could include ...
21
votes
6answers
16k views
Is PHP Object-oriented?
Is PHP an object-oriented language? If not, then what about the framework CakePHP? Is it an object-oriented MVC implementation of PHP?
Also, can a PHP application wholly built using classes be called ...
20
votes
13answers
1k views
Classes. Whats the point?
I'm fairly new to OOP in PHP, I've made a couple of basic scripts but nothing impressive. All I've really taken from it is that it would probably be easier just make a collection of functions and ...
20
votes
10answers
5k views
Static classes in PHP via abstract keyword?
According to the PHP manual, a class like this:
abstract class Example {}
cannot be instantiated. If I need a class without instance, e.g. for a registry pattern:
class Registry {}
// and later:
...
20
votes
5answers
3k views
PHP - best way to initialize an object with a large number of parameters and default values
I'm designing a class that defines a highly complex object with a ton (50+) of mostly optional parameters, many of which would have defaults (eg: $type = 'foo'; $width = '300'; $interactive = false;). ...
19
votes
7answers
23k views
In php when initializing a class how would one pass a variable to that class to be used in its functions?
So here is the deal. I want to call a class and pass a value to it so it can be used inside that class in all the various functions ect. ect. How would I go about doing that?
Thanks,
Sam
19
votes
2answers
7k views
Can PHP instantiate an object from the name of the class as a string?
Is it possible in PHP to instantiate an object from the name of a class, if the class name is stored in a string?
19
votes
5answers
6k views
Autoloader for functions
Last week I learned that classes can be included in your project by writing an __autoload() function. Then I learned that using an autoloader isn't only a technique but also a pattern.
Now I'm using ...
18
votes
6answers
15k views
dynamic class property $$value in php
How can i reference a class property knowing only a string?
class Foo
{
public $bar;
public function TestFoobar()
{
$this->foobar('bar');
}
public function ...
18
votes
3answers
30k views
“call to undefined function” error when calling class method
this is the error Fatal error: Call to undefined function assign(
this is the code, as you can see i obviously have defined the function so why is it not working
class shades {
function ...
18
votes
1answer
9k views
PHP: Get method's arguments?
In php I can check all available methods for an object like so:
$methods = get_class_methods($object);
But how can I see wich arguments have to be sent to these methods?
Is there a function for ...
18
votes
1answer
47k views
Call-time pass-by-reference has been removed [duplicate]
Possible Duplicate:
Call-time pass-by-reference has been deprecated;
while it may be documented somewhere on the Internet.. I cannot find a solution to my problem. Since the update to PHP ...
18
votes
5answers
5k views
Can I use string concatenation to define a class CONST in PHP?
I know that you can create global constants in terms of each other using string concatenation:
define('FOO', 'foo');
define('BAR', FOO.'bar');
echo BAR;
will print 'foobar'.
However, I'm getting ...
17
votes
5answers
24k views
Can I include code into a PHP class?
I want to make a PHP class, lets say Myclass.php. Now inside that class I want to define just the class itself and some instance variables. But all the methods must come from a Myclass_methods.php ...
17
votes
6answers
593 views
To inject or to new?
With regards to using class objects within another class what is the best practice? To pass the class objects in the class _construct statement or create a new class object?
Example 1:
class Foo {
...
16
votes
3answers
23k views
PHP Static class initializer
I have an helper class with some static functions. all the functions in the class requires a 'heavy' initialization function to run once ( like it was a constructor.. ).
is there a good practice ?
...
16
votes
6answers
21k views
How can I call a static method on a variable class?
I'm trying to make some kind of function that loads and instantiates a class from a given variable. Something like this:
<?php
function loadClass($class) {
$sClassPath = ...
15
votes
8answers
265 views
I'm new to OOP/PHP. What's the practicality of visibility and extensibility in classes?
I'm obviously brand new to these concepts. I just don't understand why you would limit access to properties or methods. It seems that you would just write the code according to intended results. Why ...
15
votes
6answers
13k views
Returning a value in constructor function of a class
So far I have a PHP class with the constructor
public function __construct ($identifier = NULL)
{
// Return me.
if ( $identifier != NULL )
{
$this->emailAddress = $identifier;
if ...
15
votes
5answers
5k views
logic behind pagination like google
What is the logic behind google's pagination bahviour?
My paginator goes something like this:
[1] 2 3 ... 184 >
< 1 [2] 3 4 ... 184 >
< 1 2 [3] 4 5 ... ...
14
votes
3answers
15k views
Converting a PHP array to class variables
Simple question, how do I convert an associative array to variables in a class? I know there is casting to do an (object) $myarray or whatever it is, but that will create a new stdClass and doesn't ...
14
votes
3answers
8k views
Extending PHP static classes
I've been struggling in this area for days now, and I have reached a conclusion, but since the conclusion was not what I was looking for, before I give up, I'll try to see what other people say. Faith ...
14
votes
2answers
620 views
Derived class defined later in the same file “does not exist”?
Let’s suppose we’ve got two php files, a.php and b.php
Here’s content of file a.php:
<?php // content of a.php
class A {
}
And here’s the content of file b.php
<?php // content of b.php
...
13
votes
10answers
1k views
Is there reason to create class for single function?
I've seen a lot of classes with a SINGLE function in it. Why do they put a SINGLE function into class?
I use classes just to make things more clear, but about those who put a SINGLE function into ...
13
votes
5answers
1k views
get parent extends class in php
i have the oop php code:
class a {
// with properties and functions
}
class b extends a {
public function test() {
echo __CLASS__; // this is b
// parent::__CLASS__ // error
...
13
votes
3answers
34k views
Best practice on PHP singleton classes [duplicate]
Possible Duplicate:
Who needs singletons?
I always write with respect to best practice, but I also want to understand why a given thing is a best practice.
I've read on in an article (I ...
0
votes
5answers
3k views
load config.php with in a class
I want to load a configuration file in a class. Here is the content of config.php
<?php
$__error_reporting_level=1;
$server='localhost';
$user='root';
$password='';
$dbase='eauction';
?>
...
0
votes
2answers
297 views
How do I access parent property from subclass? PHP
I have an issue accessing top level variables from sub-level class.
Here's an example...
Application.php:
class Application {
var $config;
var $db;
function __construct() {
...
0
votes
1answer
12 views
select from db with phpoo
i guys, where i am again with noob questions
i made before 1 page alone for select data from my db and it works, is this the code:
<?php
$ligacao = mysql_connect("localhost", "root", "") or ...
-1
votes
0answers
26 views
Fatal error: Call to a member function column() on a non-object
A very strange behavior.
I define an object; check if that is an object. But whe I try to execute a method, php says me that is an error.
$header = $report->addStructure( ...etc
var_dump( ...