php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25687 "Segmentation fault" - trying to inherit >2 classes and to call parent constr.
Submitted: 2003-09-29 05:20 UTC Modified: 2003-10-02 17:04 UTC
From: kostyal at realeastnetworks dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5CVS, 4CVS OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kostyal at realeastnetworks dot com
New email:
PHP Version: OS:

 

 [2003-09-29 05:20 UTC] kostyal at realeastnetworks dot com
Description:
------------
Description:
Segmentation fault occurs when trying to inherit more then two classes and to call parent constructors (see example to understand how I'm trying to do it).
When I comment last "call_constr()" call, the problem disappears.


Configure Command:
'../configure' '--prefix=/usr' '--with-apxs=/usr/bin/apxs' '--with-regex=php' '--with-config-file-path=/etc/php4/apache' '--disable-rpath' '--disable-debug' '--enable-memory-limit' '--with-layout=GNU' '--with-pear=/usr/share/php' '--enable-calendar' '--enable-sysvsem' '--enable-sysvshm' '--enable-track-vars' '--enable-trans-sid' '--enable-bcmath' '--with-bz2' '--enable-ctype' '--with-db4' '--with-iconv' '--enable-exif' '--enable-filepro' '--enable-ftp' '--with-gettext' '--enable-mbstring' '--with-pcre-regex=/usr' '--enable-shmop' '--enable-sockets' '--enable-wddx' '--disable-xml' '--with-expat-dir=/usr' '--enable-yp' '--with-zlib' '--without-pgsql' '--with-kerberos=/usr' '--with-openssl=/usr' '--with-exec-dir=/usr/lib/php4/libexec' '--disable-static' '--with-curl=shared,/usr' '--with-dom=shared,/usr' '--with-dom-xslt=shared,/usr' '--with-dom-exslt=shared,/usr' '--with-zlib-dir=/usr' '--with-gd=shared,/usr' '--with-jpeg-dir=shared,/usr' '--with-xpm-dir=shared,/usr/X11R6' '--with-png-dir=shared,/usr' '--with-freetype-dir=shared,/usr' '--with-imap=shared,/usr' '--with-imap-ssl' '--with-ldap=shared,/usr' '--with-mcal=shared,/usr' '--with-mhash=shared,/usr' '--with-mm' '--with-mysql=shared,/usr' '--with-unixODBC=shared,/usr' '--with-recode=shared,/usr' '--enable-xslt=shared' '--with-xslt-sablot=shared,/usr' '--with-snmp=shared' '--enable-ucd-snmp-hack' '--with-sybase-ct=shared,/usr' '--with-ttf=shared,/usr' '--with-t1lib=shared,/usr'


Reproduce code:
---------------
class object {
	function call_constr() {
		$par=get_parent_class($this);
		$this->$par();
	}
}
class Graph extends object {
	function Graph() {
	}
}
class TwinGraph extends Graph {
	function TwinGraph() {
		$this->call_constr(); // calling parent constructor
	}
}
class TwinBarChart extends TwinGraph {
	function TwinBarChart() {
		$this->call_constr(); // calling parent constructor
		echo "Test";
	}
}

$graph=new TwinBarChart();

Expected result:
----------------
Test

Actual result:
--------------
Segmentation fault

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-10-02 17:04 UTC] moriyoshi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

get_parent_class($instance) returns the parent class name of the specified instance. Thus, whatever call_constr() is invoked from, get_parent_class() always returns the same value since the actual class of $this is TwinBarChart, and this leads call_constr() into an infinite recursion which finally causes segmentation fault.

As an alternative solution, you can do it like the following:

<?php
class object {
    function call_constr($class) {
        $par = get_parent_class($class);
        $this->$par();
    }
}
class Graph extends object {
    function Graph() {
    }
}
class TwinGraph extends Graph {
    function TwinGraph() {
        $this->call_constr(__CLASS__);
    }
}
class TwinBarChart extends TwinGraph {
    function TwinBarChart() {
        $this->call_constr(__CLASS__);
        echo "Test";
    }
}
?>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 06:01:29 2024 UTC