March 25, 2012

Working with extended classes in PHP

Question by epic_syntax

I have 2 classes looking like this:

class db {

    protected $db;

    function __construct() {
        $this->connect();
    }

    protected function connect() {
        $this->db = new MySQLi(db_host, db_user, db_pass, db_name) or die($this->db->error);
        $this->db->set_charset('utf8');
    }

}

and

class sample extends db {

    protected $js_base_dir;

    public function __construct($js_base_dir = js_dir) {
        $this->js_base_dir = $js_base_dir . "/";
    }
 ....

I want to use $this->db inside second class, but __construct in sample class overrides first classes construct function. How to get $this-> db inside second class? Am I doung something wrong? if yes what’s proper way?

Answer by Starx

You can call parent class method, using parrent::methodName(). Similarly, you can use this to invoke parent’s constructor method as well as

parent::__construct();

Usage:

public function __construct($js_base_dir = js_dir) {
    parent::_construct();
    $this->js_base_dir = $js_base_dir . "/";
}

Apart from the manual read this article for extended explanation.

March 3, 2012

Folder with Get value

Question by Smile Applications

Could someone with more experience than me explain how it works the link (for example):

http://www.facebook.com/zuck  

I think it’s the same thing of this

http://www.facebook.com/profile.php?id=4

I imagine that “zuck” is a GET type string but I don’t understand how I can do the same thing.

Thank You very much

Answer by ilya iz

.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?id=$1 [L]

Answer by Starx

Actually, i am not sure about it, but by the way I see it, facebook probably uses both ways to get to a profile

I quick .htaccess to make sure all the request arrive on same page.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?input=$1 [L]

Now, in the profile.php, it should do a simple check like

$input = $_GET['input'];

if(is_string($input)) {
 // then retrieve profile id, based on the string
}
//now either way you have an unique identifier at last
//
//
// use your logic further more
December 19, 2010

When uploading, where do I set the php.ini parameters?

Question by gAMBOOKa

In the script where the upload form is, or the script that processes the action?

Example parameters:

memory_limit

EDIT: Ok, let me clarify. First, I didn’t know the 2nd, 3rd and 4th parameter could not be changed using ini_set(). Now, I want to know where exactly I should call the ini_set() function. In the form script or the action handling script?

Here’s an example to illustrate what I’m looking for:

form.php

<form action="post.php">
  <input type="text" name="search" />
  <input type="submit" />
</form>

post.php

<?php //handle search ?>

So under which file must my ini_set() function should go?

Answer by Starx

You have to set these parameters initially from php.ini. You cannot change this from a PHP script except memory_limit, for the remaining Just search for those keys you mentioned in php.ini and change their values.

For memory_limit you can use following snippet at your processing page handling the uploads like in your case at post.php

ini_set("memory_limit","16M");

OR

You can create a .htaccess file at the root directory, if your server support mod_rewrite modules you can change them like this

php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
...

Please fill the form - I will response as fast as I can!